How to detect whether HTML5 video has paused for buffering?

后端 未结 5 1771
长情又很酷
长情又很酷 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:23

    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
    }
    

提交回复
热议问题