How to detect whether HTML5 video has paused for buffering?

后端 未结 5 1770
长情又很酷
长情又很酷 2021-01-30 06:45

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 det

5条回答
  •  一向
    一向 (楼主)
    2021-01-30 07:21

    As per MDN docs 'waiting' event is -

    Sent when the requested operation (such as playback) is delayed pending the completion of another operation (such as a seek).

    So seek or network request will trigger 'waiting'. Michael in the comments did point out that 'waiting' is reliable as of 2019 so I gave this a try and it worked!

    let slowInternetTimeout = null;
    
    let threshold = 3000; //ms after which user perceives buffering
    
    video.addEventListener('waiting', () => {
        slowInternetTimeout = setTimeout(() => {
            //show buffering
        }, threshold);
    });
    video.addEventListener('playing', () => {
        if(slowInternetTimeout != null){
            clearTimeout(slowInternetTimeout);
            slowInternetTimeout = null;
        }
    });
    

提交回复
热议问题