I am using some nice html5 video background for div, but on very slow connections, it is pausing for buffering every second. I want to create such logic: start playing after 10s buffered, and if playback is faster and it start buffering, smoothly seek video to begin (or to buffered range), to allow more buffering and don't stop playback (position doesn't matter much).
So the goal is have a forever-playing looping video, even if buffer is low
I found some code in this question: How to detect whether HTML5 video has paused for buffering?
And tried it in such way:
var checkInterval = 50.0
var lastPlayPos = 0
var currentPlayPos = 0
var bufferingDetected = false
setInterval(checkBuffering, checkInterval)
function checkBuffering() {
var player = document.getElementById('bgvideo')
currentPlayPos = player.currentTime
// checking offset, e.g. 1 / 50ms = 0.02
var offset = 1 / checkInterval
// 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
player.currentTime=0; //seek to begin
}
// 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
}
It doesn't work well, because when seeking to begin, buffer is flushed sometimes and it start buffering from begin. The seeking is not smooth and give a 2 sec pause, also this code don't resolve 10s prebuffer on beginning.
I've heared about MSE, but I am not sure it is supported everywhere and is very hard to understand.
Experts, please help!