[英]JS Audio onended not triggering when audio ends
I'm trying to get a function to fire when the audio has ended. 我正在尝试让音频结束时触发的功能。 I am using this function to create the sound. 我正在使用此功能来创建声音。
function sound(src,volume,repeat) {
volume = volume || 1
repeat = repeat || false;
this.sound = document.createElement("audio");
this.sound.volume = volume;
this.sound.src = src;
this.sound.setAttribute("preload", "");
this.sound.setAttribute("class", "gamesound");
if(repeat)this.sound.setAttribute("loop","");
this.sound.style.display = "none";
document.body.appendChild(this.sound);
this.play = function(){
this.sound.play();
}
this.stop = function(){
this.sound.pause();
}
this.pause = function(){
this.sound.pause();
}
this.onend = function (s){
this.sound.onended = s;
}
this.load = function(){
this.sound.load();
}
this.currentTime = function (t){
this.sound.currentTime = t;
}
}
So it can be used like this 所以可以这样使用
var song = new sound("sound.mp3",0.2,true);
song.ended(function(){
alert("ended");
})
But its not triggering, where am i going wrong here. 但是它没有触发,我在这里哪里出错了。
如果启用了循环选项,则不会触发该事件,在这种情况下,您正在演示该事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.