Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to play sound on iphone using JavaScript but able to play on Android

I am trying to play a sound notification on android and iPhone using JavaScript library but I am only able to play the sound on android and not on iPhone.

I am using JavaScript ion.sound.play("beep") this function to play the sound. I have kept the sound file in my folder. Can any one help me out; how can I play sound on iPhone using JavaScript?

like image 727
Vijay Patil Avatar asked Dec 14 '25 03:12

Vijay Patil


1 Answers

The sounds require user input in iOS9. You can initialize the sound with an "invisible" button that disappears after one press at the initialization of your app. Once triggered, you should be able to call ion.sound.play("beep") programatically. I will help You.

CSS:

<style>
    #init {
        position: absolute;
        margin-right: 0px;
        margin-left: 0px;
        margin-top: 0px;
        margin-bottom: 0px;
        z-index: 1000;
    }
</style>

JAVASCRIPT:

if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {

    document.write('<a id="init" ontouchstart="javascript:sndInit();"></a>');

    function sndInit(){
    ion.sound.play("beep");
    ion.sound.pause("beep");
    document.getElementById('init').style.display = 'none';
    }
}
like image 79
Anuj J Avatar answered Dec 16 '25 18:12

Anuj J



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!