MDN says:
The pause event is sent when a request to pause an activity is handled and the activity has entered its paused state, most commonly after the media has been paused through a call to the element’s pause() method.
But what if the audio stops because it stalled, or on an ended event.
Would it still trigger the pause
event? Or should I also perform actions on stalled
and ended
?
Advertisement
Answer
The Audio Pause Event
The answer is that the pause event is only emitted after the pause()
method is called or BEFORE an ended
or seeking
event. The seeking event happens when the track position is changed, which causes the player to toggle between pause and play.
The pause event is NOT emitted following a stalled
or waiting
event because the audio is still in a play state. Also, when the audio src is changed it stops the player, but no pause event is emitted.
The w3.org event specifications don’t provide much detail and implementation may vary by browser.
Run the code snippet to monitor audio events on your browser.
"audioprocess,canplay,canplaythrough,complete,durationchange,emptied,ended,loadeddata,loadedmetadata,pause,play,playing,ratechange,seeked,seeking,stalled,suspend,timeupdate,volumechange,waiting".split(",").forEach(name => { player.addEventListener(name, (e) => monitor.innerHTML= e.timeStamp.toFixed(2) + ": " + e.type + "n" + monitor.innerHTML); });
<small>Click the audio controls to monitor events:</small><br/> <audio id="player" controls preload="false" src="http://commondatastorage.googleapis.com/codeskulptor-assets/Epoq-Lepidoptera.ogg" type="audio/ogg"> </audio> <textarea id="monitor" rows=6 style="width:100%"></textarea>