Skip to content

Instantly share code, notes, and snippets.

@tylerpaige
Last active August 26, 2015 18:24
Show Gist options
  • Save tylerpaige/d0273553f7d5a4387202 to your computer and use it in GitHub Desktop.
Save tylerpaige/d0273553f7d5a4387202 to your computer and use it in GitHub Desktop.
Many browser handle the HTML media event "waiting" differently... To me, it seems this event should fire when media has been paused to allow for buffering. This function will supply that event with a fair amount of accuracy.
/* This function leverages the more cross-browser compatible event `timeupdate`
to see if the currentTime value has changed since the media supposedly starting
playing. The `timeupdate` event fires ~3/second, so every half second, we check
if the currentTime has indeed changed. If it hasn't we trigger a jQuery custom event
See this issue on the video.js repo for more information regarding this bug:
https://github.com/videojs/video.js/issues/1318 */
function listenForWaiting(media) {
var waiting = false;
var playing = false;
var checkInterval;
var check = function() {
return setInterval(function() {
if(!waiting && !playing) {
waiting = true;
$(player).trigger('waiting');
} else if(waiting && playing) {
waiting = false;
$(player).trigger('resumed');
}
playing = false;
}, 500);
};
var stopCheck = function() {
clearInterval(checkInterval);
playing = false;
return checkInterval;
};
media.addEventListener('timeupdate', function() {
playing = true;
});
media.addEventListener('play', function() {
checkInterval = check();
});
media.addEventListener('pause', function() {
stopCheck();
});
media.addEventListener('seeked', function() {
stopCheck();
if(!player.paused) {
checkInterval = check();
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment