简体   繁体   English

触摸iPad / iPhone设备上的HTML5音频会停止

[英]HTML5 Audio on an iPad/iPhone device stops when being touched

In order to get an HTML5 animation playing with sound on an idevice, I've made a div the size of the entire browser named "theScreen", and use the following code: 为了使HTML5动画在idevice上播放声音,我将整个浏览器的大小定为div,命名为“ theScreen”,并使用以下代码:

audioCont.prototype.iCrapLoadPlayThrough = function () { 
    if (this.supported) {
        theScreen = document.getElementById("theScreen");
        var self = this;
        theScreen.addEventListener('touchstart', function(){self.iCrapClickedLoadPlayThrough();}, false);
        return(1);
    } else {
        return(0); // Not supported
    }
};
audioCont.prototype.iCrapClickedLoadPlayThrough = function () { // Check if supported, then load the audio file
    var self = this;
    theScreen.removeEventListener('touchstart', function(){self.iCrapClickedLoadPlayThrough();}, false);
    this.addCanPlayThrough();
    this.load();
}

Now this works, and the sound/animation starts when the user taps on the screen. 现在可以使用,当用户在屏幕上点击时,声音/动画就会开始。 The problem is, if they tap on it again the sound stops, and each repeat tap you hear a few ms of audio. 问题是,如果他们再次点击它,声音就会停止,并且每次重复点击都会听到几毫秒的音频。 Does anyone know why? 有人知道为什么吗?

It's not removing the event listener because it's an anonymous function. 它不是删除事件监听器,因为它是一个匿名函数。 Remove the anonymous function and just have the function name instead 删除匿名函数,仅使用函数名

 theScreen.addEventListener('touchstart',self.iCrapClickedLoadPlayThrough,false)
 theScreen.removeEventListener('touchstart',self.iCrapClickedLoadPlayThrough,true)

I've found a solution to the problem: 我找到了解决该问题的方法:

theScreen.addEventListener('touchstart', eventID=function() {self.iCrapClickedLoadPlayThrough();}, false);

then

theScreen.removeEventListener('touchstart', eventID, false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM