Control start position and duration of play in HTML5 video

前端 未结 4 1101
悲&欢浪女
悲&欢浪女 2021-01-05 01:37

We have a video (13 minutes long) which we would like to control using HTML5. We want to be able to let our users control and select the parts of the video they want to pla

4条回答
  •  生来不讨喜
    2021-01-05 02:04

    You could use the timeupdate event listener.

    Save the start time and duration time to variable after loadedmetadata event.

    // Set video element to variable
    var video = document.getElementById('player1');
    
    var videoStartTime = 0;
    var durationTime = 0;
    
    video.addEventListener('loadedmetadata', function() {
      videoStartTime = 2;
      durationTime = 4;
      this.currentTime = videoStartTime;
    }, false);
    

    If current time is greater than start time plus duration, pauses the video.

    video.addEventListener('timeupdate', function() {
      if(this.currentTime > videoStartTime + durationTime){
        this.pause();
      }
    });
    

提交回复
热议问题