Youtube player api with loop

后端 未结 7 1098
伪装坚强ぢ
伪装坚强ぢ 2020-12-31 02:24

I am struggling with setting up the loop for youtube videos using youtube player api.

The problem is that the video is not running under a loop.

Here is my c

7条回答
  •  北荒
    北荒 (楼主)
    2020-12-31 02:55

    Since the html5 player, we can just right click on the video => play in loop.

    From script, to replay at the end of the video:

    document.getElementsByClassName('video-stream html5-main-video')[0].loop = true
    

    Loop 1.66 seconds between two time points:

    const video = document.getElementsByClassName('video-stream html5-main-video')[0]
    
    /* Loop markers in seconds */
    let start = 54.34, end = 56
    
    /* Seek to time in seconds */
    function seek(time){
      video.loop = true
      video.currentTime = time
      video.play()
    }
    
    /* Loop between start and end */
    function loop(e){
      if (e.target.currentTime > end){
        seek(start)
      }
    }
    
    /* Will loop for 1.66s */
    video.addEventListener("timeupdate", loop, false)
    
    /* Seek to 54.34s */
    seek(start)
    
    /* Abort the loop */
    // video.removeEventListener("timeupdate", loop, false)
    
    /* New loop */
    // start = 14; end = 15; seek(start)
    

    Media events list

    HTMLMediaElement reference

提交回复
热议问题