Alex Rivera | Logout

How can I tell when an HTML5 audio element has finished playing?

Asked 2012-02-19T04:06:10.840
47

How can I tell when an HTML5 audio element has finished playing?

Does it throw events I can listen to or something similar? I need to react when an audio track ends.

Thanks!

Edit
Report

1 Answer

88

Using HTML:

<audio id="music" src="blah.mp3" onended="yourFunction()"></audio>

Using Javascript:

document.querySelector("#music").addEventListener("ended", yourFunction, false);

Using jQuery:

$("#music").on("ended", yourFunction);

Read more about Media events on MDN

answered 2012-02-19T04:16:37.230

Your Answer