HTML5 video within time frame

别来无恙 提交于 2019-12-12 01:29:22

问题


So I am working on an interactive HTML5 video player, and currently have everything working while using popcorn.js, but I am now trying to go back and make my player work without being dependent on popcorn.js.

When working with popcorn.js, you can use code like:

popcorn.code((
    start: 0,
    end: 5,
    onStart: function( options ) {
        //put code here
    }
}}

and your code will be executed when the time of your video spans from 0 through 5. Now, I am trying to have certain blocks of code executed within a certain timeframe of the video, but i can't seem to get it working right and was just wondering if someone can see where i am going about this wrong.

while (myVideo.currentTime >= 0 && myVideo.currentTime <= 5)
{
    console.log(myVideo.currentTime);
}

This while loop is also running so fast that it causes the browser to slow down and freeze. However, if i try using an if loop instead of a while loop, it (obviously) only checks it once.


回答1:


You could check fewer than the while loop would.

var check = setInterval(function() {
    if (myVideo.currentTime >= 5) {
        clearInterval(check);
        console.log("5 seconds reached");
    }
}, 500);

You than can start this again when the user pauses and starts over or if he jumps to another time within the timeline.




回答2:


Try using the following so your function will run only once every second.

setInterval(function(){
if(myVideo.currentTime >= 0 && myVideo.currentTime <= 5){
console.log(myVideo.currentTime);
}
},1000);

Good luck!



来源:https://stackoverflow.com/questions/14651302/html5-video-within-time-frame

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