

Of course the "loadedmetadata" event will only fire at all if the media is able to load - if it fails (for example, if the src returns a 404), then the media will instead produce an "error" event, and no further playback will be possible. duration (as a floating-point number, rather than its default value of NaN). The "progress" event is rather complex, so we’ll look at that separately in the next section, but the "loadedmetadata" event is straightforward, as it simply means that the browser has loaded enough meta-data to know the media’s. (Without preloading these events will still fire, but not until playback begins.) Whereas if the preload attribute has the value "metadata" or "auto", then two more events will fire quite soon, which are "progress" and "loadedmetadata". If the preload attribute has the value "none", then the "loadstart" event is the only one that will fire before playback begins. But that’s all it means - it doesn’t mean any data has actually loaded, or that the media resource even exists.

The first to fire in all cases is the "loadstart" event, which means that the browser has begun to look for data. whether the preload attribute is used and/or whether the media is already cached. The prevalence of these events depends on the loading state of the media, i.e. The loading events are those which fire in respect of loading (or failing to load) media data. the button would do nothing at all): button.addEventListener('click', function(e)īut you can fix this quirk quite easily, by firing the pause() method manually in response to the "ended" event: media.addEventListener('ended', function(e)įirefox and Chrome already fix this internally, and in exactly the same way - by firing a "pause" event just before the "ended" event. A practical upshot of this is that a simple play/pause button handler like this would fail in that situation (i.e. paused flag remains false when the media has ended (yet logically, it should be true since the media is no longer playing). However there is a significant anomaly here in Opera, Safari and IE10, which is that the. at the same time as the "ended" event fires). ended property is false by default, but then becomes true when playback reaches the end (i.e. paused property is true by default, or whenever the media is paused, while the. There are also two media properties that correspond with the last two events - the. There are media functions that correspond with the first two events - unsurprisingly called play() and pause(). The "play" and "pause" events fire when the media is played or paused (respectively), but there’s also an "ended" event which fires when the media reaches the end - either because ordinary playback has finished, or because the user manually “seeked” that far. Keeping the timeupdate binding in there means that if the playhead updates through some method other than playing (such as seeking), my onAudioUpdate function gets called anyway.The playback events are those which fire in response to playing or pausing the media. Within that function I use another anonymous function because my onAudioUpdate function is otherwise outside the scope of the setInterval, and so would otherwise appear to be onAudioUpdate undefined.īy tying the start/stop of the setInterval clock to the audio elements' play and pause events, it has similar functionality to the timeupdate event. In the play even binding, I use an anonymous function so that I could assign to the audio_clock variable with something a bit complicated. $(audiohtml).bind("timeupdate", onAudioUpdate) Instead, I used a 10Hz setInterval clock to trigger the movement functions, and used the audio's play and pause events to create and destroy the clock: $(audiohtml).bind("pause", onAudioUpdate) īut the trouble was that the audio element's timeupdate event only fires once every 200ms or so on my browser (this is set by the HTML5 audio specification and isn't modifiable as far as I know).Ģ00ms was slow enough that the animation of the elements looked jerky (5 fps). $(audiohtml).bind("play", onAudioUpdate) I was trying to make a bunch of elements change position as an audio file played, using a snippet something like: $(audiohtml).bind("timeupdate", onAudioUpdate)
