How to detect whether HTML5 video has paused for buffering?

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

    You can just check the buffered video content length and if it is less than the current playing part then just fire the pause event.Using following code you can check the buffered video length.

    $vid = $("#video_id");
    
    $vid.on('progress', function(e) {
    
        percentVidLoaded = null;
        // FF4+, Chrome
        if ($vid[0] && $vid[0].buffered && $vid[0].buffered.length > 0 && $vid[0].buffered.end && $vid[0].duration) {
            percentVidLoaded = $vid[0].buffered.end(0) / $vid[0].duration;
        }
        /* Some browsers (e.g., FF3.6 and Safari 5) cannot calculate target.bufferered.end()
         *  to be anything other than 0. If the byte count is available we use this instead.
         *  Browsers that support the else if do not seem to have the bufferedBytes value and
         *  should skip to there.
         */
        else if ($vid[0] && $vid[0].bytesTotal != undefined && $vid[0].bytesTotal > 0 && $vid[0].bufferedBytes != undefined) {
            percentVidLoaded = $vid[0].bufferedBytes / $vid[0].bytesTotal;
        }
        if (percentVidLoaded !== null) {
            percentVidLoaded = 100 * Math.min(1, Math.max(0, percentVidLoaded));
        }
    });
    

提交回复
热议问题