7

I have not seen much documentation on the <audio> tag in HTML5. I am trying to trigger an event when audio has finished playing.

HTML:

<audio id="audio">
    <source src="'.$query['audioPronunciation'].'" type="audio/mp3">
    Your browser does not support the audio element.
</audio>

JavaScript / jQuery Play & Pause:

function audio() {
    if ($("#audioControl").hasClass("glyphicon-play")) {
        $("#audioControl").addClass("glyphicon-pause").removeClass("glyphicon-play");
        $("#audio").trigger("play");
    } else if ($("#audioControl").hasClass("glyphicon-pause")) {
        $("#audioControl").removeClass("glyphicon-pause").addClass("glyphicon-play");
        $("#audio").trigger("pause");
    }
}

JavaScript / jQuery, 'when ended'

$("#audio").addEventListener('ended', function() {
    alert("Audio has finished playing!");

});

1 Answer 1

9

You don't need the event listener, just:

var audio1 = document.getElementById("myAudio1");
audio1.onended = function() {
    alert("audio playback has ended");
};
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.