0

I am trying to bind an event where when the audio finished playing, it would fire a certain function, but however, when I bind the event it says aux.bind is not a function.

var aux = new Audio("file.mp3");
aux.bind("ended", function(e){
    aux.volume = 1.0;
});

I have even tried aux.on(...) and it does not work either.

UPDATE: I will require to unbind the event later after the "ended" has fired

Daniel Hyuuga
  • 446
  • 1
  • 5
  • 13
  • Try [`onended()`](http://www.w3schools.com/tags/av_event_ended.asp)? – Jonathan Lam Jan 21 '16 at 14:28
  • I think you may be trying to use jQuery's `bind()` function. Make sure you have included the jQuery library. If I'm not mistaken, the [plain JS `bind()` is different](http://stackoverflow.com/a/10115970/2397327). Therefore, try the `.onended()` event handler. – Jonathan Lam Jan 21 '16 at 14:31
  • Can you explain the unbind part more? What's the reason to unbind the event? – gothical Jan 21 '16 at 14:48
  • @gothical the unbind is for to alter the volume, as the audio will be fired frequently, so that is why I would like to unbind the event later. – Daniel Hyuuga Jan 21 '16 at 14:52

1 Answers1

1

Use the onended() event from the Audio object. Eg.

var aux = new Audio("file.mp3");
aud.play();
aud.onended = function() {
    alert("The audio has ended");
};
gothical
  • 373
  • 1
  • 7