26

Is there an oncomplete event for HTML5 audio? I could not find anything on this. I am trying to play a sound after it completes.

Well, specifically I am trying to play through an array of sounds one after the other.

Thanks.

Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
james
  • 2,595
  • 9
  • 43
  • 70

2 Answers2

32

There is the ended event . . .

http://dev.w3.org/html5/spec/Overview.html#event-media-ended

Example:

var audioElement = document.getElementById("myAudio");

audioElement.addEventListener('ended', function() {
    // Audio has ended when this function is executed.
},false);
M -
  • 26,908
  • 11
  • 49
  • 81
the JinX
  • 1,950
  • 1
  • 18
  • 23
  • 5
    Multi-page spec link, if your browser’s struggling with the mighty one-page spec: http://dev.w3.org/html5/spec/video.html#event-media-ended – Paul D. Waite Feb 23 '11 at 14:43
  • Ok thanks.I guess this does not work in the chrome browser?var snd1 = new Audio("sounds/GunShot.mp3");snd1.onended = function(e){ snd1.play(); } – james Feb 23 '11 at 14:51
  • http://code.google.com/p/chromium/issues/detail?id=30452 http://code.google.com/p/chromium/issues/detail?id=45074 They seem to be working on it . . – the JinX Feb 23 '11 at 14:53
  • Not sure since I don't own an Iphone to try but I do know playing audio via the html5 audio tag works (tested on G4) and the mobile safari is pretty up-to-date. . so I'd guess they should have this event handled as well. – the JinX Feb 23 '11 at 15:01
21

The correct way to to this is using the addEventListener function:

audio.addEventListener('ended', PlayNext);
Tomas Ramirez Sarduy
  • 17,294
  • 8
  • 69
  • 85