当音频结束时,JS Audio onended不会触发

问题描述 投票:0回答:1

我正试图在音频结束时启动一个功能。我正在使用此功能来创建声音。

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

所以它可以像这样使用

var song = new sound("sound.mp3",0.2,true);
song.ended(function(){
    alert("ended");
})

但它没有触发,我在哪里错了。

javascript html5-audio
1个回答
0
投票

如果您启用了循环选项,则不会触发该事件,在此情况下您将显示该事件。

© www.soinside.com 2019 - 2024. All rights reserved.