Using the stalled event is correct, but unfortunately, it does not always work as expected.
You could use Media Source Extension which gives you full control of the buffer and allow you to detect stalls manually. However, a solution using that is a bit out of scope here IMO.
You could possible get around using the timeupdate
event as well.
- Have a
setTimeout()
running with a time-out value
- Inside the
timeupdate
event, clear this timer and set a new
- If the timer isn't reset, it means there is no time progress and if not paused or ended, assume stalling
Example (untested):
...
var timerID = null, isStalling = false;
vid.addEventListener("timeupdate", function() {
clearTimeout(timerID);
isStalling = false;
// remove stalling indicator if any ...
timerID = setTimeout(reportStalling, 2000); // 2 sec timeout
});
// integrate with stalled event in some way -
vid.addEventListener("stalled", function() {isStalling = true})
function reportStalling() {
if ((!vid.paused && !vid.ended) || isStalling) { ... possible stalling ... }
}
...
You may have to do some additional checks to eliminate other possibilities and so forth, but this is only to give you the general idea, and in addition to using the stalling
event.
A different approach could be to monitor the loaded buffer segments using the buffered
object (see this answer here for example on usage).
These can be used to see if you have any progress, then use currentTime
compared with the ranges to see if the time is at the end of a range and/or if the ranges are changing at all.
In any case, hope this give some input.