Detect in Javascript when HTML5 <video> loop restarts?

后端 未结 3 1807
一个人的身影
一个人的身影 2021-02-07 12:37

I have a looping HTML5 video using , and I want to know when the video loops. The event listener play only fires when the vi

3条回答
  •  悲哀的现实
    2021-02-07 12:48

    I'm not sure how robust this method is but something I've found through

    player.on("timeupdate", function(e) {
        console.log(player.currentTime)
    });
    

    is that if a user scrubs to 0 time, the timeupdate event fires twice with currentTime==0 and if the video loops it fires timeupdate 3 times with currentTime==0. This seems to hold across FF, Chrome, IE but I wonder if it's related to common implementation of spec or is hardware dependent. i.e. speedy machine 3 ticks, slow machine 5 ticks of currentTime==0

    There is also player.played which will return a TimeRanges object of ranges played. So:

    player.on("timeupdate", function(e) {
        var tr = player.played;
        var hasLoopedOnce = (tr.end(tr.length-1)==player.duration);
        console.log(hasLoopedOnce);
    });
    

    The problem with this is that the ranges wont reset after each iteration so it's only good for detecting the first loop. I've tried player.played=null but to no avail.

    But using these methods to set flags and combining with player.currentTime <=0 may be enough for some scenarios if indeed just player.currentTime <=0 is not enough.

提交回复
热议问题