通过iOS 5 HTML5应用中的javascript事件播放声音

cro*_*ies 4 javascript jquery mobile-safari ios5

在新iPad的Mobile Safari iOS 5上开发HTML 5应用程序,在收到"发送"时显示用户提示.为了引起用户的注意,当调用警报功能时,我们会发出类似这样的声音:

snd.src = snd.src;
snd.play();
Run Code Online (Sandbox Code Playgroud)

不幸的是,Apple改变了播放声音的方法.有谁知道iOS 5中是否有更新解决了这个问题?

*进行了大量的研究并且已经意识到iOS 3中的旧"黑客"不再有效.

Jos*_*iah 14

声音需要iOS5中的用户输入.您可以使用"隐形"按钮初始化声音,该按钮在应用初始化时按一下后消失.一旦触发,您应该能够以snd.play()编程方式调用.这是一种欺骗,但希望它有所帮助!

使用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(){
    snd.play();
    snd.pause();
    document.getElementById('init').style.display = 'none';
    }
}
Run Code Online (Sandbox Code Playgroud)

CSS:

<style>
    #init {
        position: absolute;
        margin-right: 0px;
        margin-left: 0px;
        margin-top: 0px;
        margin-bottom: 0px;
        z-index: 1000;
    }
</style>
Run Code Online (Sandbox Code Playgroud)

另外,Remy Sharp在这里有一些关于使用音频精灵的技巧.