Play Html5 video and pause at specific points in time

て烟熏妆下的殇ゞ 提交于 2019-12-11 14:03:11

问题


I have an array of points in time (seconds) like this :

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

I want the video to play from 1 to 5 and then pause. Then some event happens (like button click) and it plays from 5 to 7 and then pauses and so on. How can I do this using js/jquery ?


回答1:


Just queue up the times (currentStopTime) and check the currentTime of the video. If currentTime >= currentStopTime then pause video, set currentStopTime to next time in the array.

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
        }
    }
}

Then when paused, enable an action to happen, simply call play() to start video again. If you need an accurate restart time then force that time by adding:

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


来源:https://stackoverflow.com/questions/36525495/play-html5-video-and-pause-at-specific-points-in-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!