Javascript iPad/iPhone设备上的HTML5音频在被触摸时停止
为了让HTML5动画在idevice上播放声音,我将整个浏览器的大小划分为“theScreen”,并使用以下代码:Javascript iPad/iPhone设备上的HTML5音频在被触摸时停止,javascript,iphone,ios,html,Javascript,Iphone,Ios,Html,为了让HTML5动画在idevice上播放声音,我将整个浏览器的大小划分为“theScreen”,并使用以下代码: audioCont.prototype.iCrapLoadPlayThrough = function () { if (this.supported) { theScreen = document.getElementById("theScreen"); var self = this; theScreen.addEven
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();
}
现在这就可以了,当用户点击屏幕时,声音/动画开始了。问题是,如果他们再次点击它,声音就会停止,每次重复点击你都会听到几毫秒的音频。有人知道原因吗?它没有删除事件侦听器,因为它是一个匿名函数。删除匿名函数,只使用函数名
theScreen.addEventListener('touchstart',self.iCrapClickedLoadPlayThrough,false)
theScreen.removeEventListener('touchstart',self.iCrapClickedLoadPlayThrough,true)
我找到了解决问题的方法:
theScreen.addEventListener('touchstart', eventID=function() {self.iCrapClickedLoadPlayThrough();}, false);
then
theScreen.removeEventListener('touchstart', eventID, false);
不幸的是,当调用子(原型)函数时,这不起作用。这就是为什么我一开始就这样编写侦听器。确实,我自己在库中使用它,但我确实喜欢您提供的解决方案。