Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect whether HTML5 video has paused for buffering?

I'm trying to test whether a video is choppy. I have noticed that the pause event is not triggered when the video pauses for buffering. What is the best way to detect whether the video has paused for buffering?

like image 295
Supreet Totagi Avatar asked Jan 28 '14 07:01

Supreet Totagi


People also ask

How do I know if my video is buffering?

You need to check if the buffer is less than the current video time. If so, then the video is buffering. However, you should check this with a small tolerance to make sure you detect it before it is acuatally necessary to buffer. The stream may stall even if the buffer end is greater than the current time.

Why is my video not working in HTML5?

If your browser error "HTML5 video file not found", it means that your browser is not up to date or website pages does not have a suitable video codec. It would help if you communicated with the developer to solve the issue and install all the required codecs.

How do I know if my video is playing jquery?

click(function() { var video = $("#myvideo"). get(0); video. play(); $(".


2 Answers

I did this by inspecting the player progress every x milliseconds, e.g. 50. If the player hasn't advanced as much as it was expected to, then we are buffering. This is quite reliable, since I've found that other events such as waiting or stalled are not fired in all cases of the video buffering.

Note that the interval must be larger than the expected inter-frame difference, but I'm sure that you won't want to be that precise anyway. An estimation of buffering time within ±300ms would still be fine, given that humans most likely cannot perceive differences in that region.

It is important to check whether the user hasn't actively paused the playback though.

var checkInterval  = 50.0 // check every 50 ms (do not use lower values) var lastPlayPos    = 0 var currentPlayPos = 0 var bufferingDetected = false var player = document.getElementById('videoPlayer')  setInterval(checkBuffering, checkInterval) function checkBuffering() {     currentPlayPos = player.currentTime      // checking offset should be at most the check interval     // but allow for some margin     var offset = (checkInterval - 20) / 1000      // if no buffering is currently detected,     // and the position does not seem to increase     // and the player isn't manually paused...     if (             !bufferingDetected              && currentPlayPos < (lastPlayPos + offset)             && !player.paused         ) {         console.log("buffering")         bufferingDetected = true     }      // if we were buffering but the player has advanced,     // then there is no buffering     if (         bufferingDetected          && currentPlayPos > (lastPlayPos + offset)         && !player.paused         ) {         console.log("not buffering anymore")         bufferingDetected = false     }     lastPlayPos = currentPlayPos } 
like image 103
slhck Avatar answered Sep 18 '22 09:09

slhck


The event you're looking for is waiting.

From the spec:

A waiting DOM event can be fired as a result of an element that is potentially playing stopping playback due to its readyState attribute changing to a value lower than HAVE_FUTURE_DATA.

The paused state does not change because the video is still "potentially playing" (i.e. "trying" to play). So the waiting event fires. When enough data has been loaded, playing fires.

You can also check the state at any time by looking at two properties, networkState and readyState

if (video.networkState === video.NETWORK_LOADING) {     // The user agent is actively trying to download data. }  if (video.readyState < video.HAVE_FUTURE_DATA) {     // There is not enough data to keep playing from this point } 
like image 36
brianchirls Avatar answered Sep 18 '22 09:09

brianchirls