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

时间:2016-04-25 09:39:41

标签: javascript jquery ios audio playback

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?

1 个答案:

答案 0 :(得分:1)

声音需要iOS9中的用户输入。您可以使用“隐形”按钮初始化声音,该按钮在应用初始化时按一下后消失。一旦触发,您应该能够以编程方式调用ion.sound.play(“beep”)。我会帮助你的。

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';
    }
}