如何在网页上流畅播放视频

How to play the video smoothly on a webpage

当我在网页中使用视频元素时,视频数据源来自另一个网站。如果网络好的话,会从那个网站抓取视频数据,如果要让视频流畅播放,视频的缓冲数据video.buffered.end(0)-video.buffered.start(0)必须长于当前时间video.currentTime, video.and我想知道是否存在这样的缓冲数据长度减去视频的currentTime`可以让视频播放流畅。

一般来说,这个数字是不存在的,但是你可以假设一个数字,比如3、5等,当缓冲的时间减去视频的当前时间大于3或5时,视频就会播放顺利,否则视频会在缓冲阶段,会卡顿,无法播放smoothly.the代码如下:

if(window.video.buffered.length !== 0){
 // assume the user have not use the progress bar to control the play progress of the video, so there is only an element in buffered array(object)
   var bufferTime = window.video.buffered.end(0) - window.video.buffered.start(0);
   var currentTime = window.video.currentTime;
// another number is OK;
   if(bufferTime - currentTime < 2){
       video.dispatchEvent(bufferingEvent);
  // write capture event function;
  }

}