播放 Html5 个视频并在特定时间点暂停

Play Html5 video and pause at specific points in time

我有一个这样的时间点(秒)数组:

var points = [1,5,7,9,23,37];

我想让视频从 1 播放到 5,然后暂停。然后发生一些事件(比如按钮点击),它从 5 播放到 7,然后暂停等等。我如何使用 js/jquery 来做到这一点?

只需排队时间(currentStopTime)并检查视频的currentTime。如果 currentTime >= currentStopTime 则暂停视频,将 currentStopTime 设置为数组中的下一个时间。

var points = [1,5,7,9,23,37],
    index = 1,
    currentStopTime = points[index];

// start video using: video.currentTime = points[0], then video.play()

// run this from a timeupdate event or per frame using requestAnimationFrame
function checkTime() {
    if (video.currentTime >= currentStopTime) {
        video.pause();
        if (points.length > ++index) {       // increase index and get next time
            currentStopTime = points[index]
        }
        else {                               // or loop/next...
            // done
        }
    }
}

然后在暂停时启用一个动作,只需调用 play() 即可再次开始播放视频。如果您需要准确的重启时间,请通过添加以下内容来强制执行该时间:

...
    if (video.currentTime >= currentStopTime) {
        video.pause();
        video.currentTime = currentStopTime;
        index++;
        ....