How to fire a jQuery event when Audio has finished playing HTML5

I'm trying to figure out how to trigger an event when my sound stops playing. I am using the HTML5 tag <audio>. So far I can get the sound to give me its duration using the property duration, but I can not do much with it at the moment. All I want to do is pass the string “play” to the html () method so that it does not show a “pause” when my sound is finished.

Toggle Function:

$("#toggleButton").on("click", function(evt) {
    if(audio.paused) {
        audio.play();
        $(this).html("Pause");
    }
    else {
        audio.pause();
        $(this).html("Play");
    }
});

I am also trying to find documentation regarding audio methods and properties. I tried to find “audio” on the JQuery and Mozilla networks, and they returned nothing.

+4
source share
2 answers

:

audio.addEventListener('ended', function(){
    $("#toggleButton").html("Play");
});
+10

"onended", . , , . , , .

+1

All Articles