Make HTML5 video stop at indicated time

后端 未结 4 1335
太阳男子
太阳男子 2021-02-07 07:45

I have a HTML5 video element in my page. The video I want to play is having a duration of 10 minutes.

I have to play the part of the video from minute

4条回答
  •  佛祖请我去吃肉
    2021-02-07 08:26

    The timeupdate event is what you are looking for, but it only fires at about 2 fps which is too slow to stop at precise times.

    For those cases I used requestAnimationFrame which fires at 60 fps and decreased the endTime a little which fixes small "lag hops":

    const onTimeUpdate = () => {
        if (video.currentTime >= (endTime - 0.05)) {
          video.pause()
        } else {
          window.requestAnimationFrame(onTimeUpdate)
        }
    }
    window.requestAnimationFrame(onTimeUpdate)
    

提交回复
热议问题