html5视频应从某个时间开始播放并循环播放

时间:2019-06-03 14:28:16

标签: javascript html video

我需要将视频循环播放50到60秒,然后单击时通常应从0循环播放到结尾。

我的功能可以在0到10秒内完全正常工作,但我需要在50到60秒内才能工作。

请提供API帮助,我尝试了loadedmetadata,但没有用。

很高兴了解我在这里缺少的东西

就像这样可行,但如果我将 50设为60

  if (this.currentTime >= 10) {
        this.currentTime = 0; // change time index here
      }

var video = document.getElementById('videoElm');

 

const playShort = function() {
  if (this.currentTime >= 60) {
    this.currentTime = 50; // change time index here
  }
};
const playFull = function() {
  if (this.currentTime >= 24) {
    this.currentTime = 0; // change time index here
  }
};

function playShortVideo() {
  video.removeEventListener("timeupdate", playFull, false)
  video.addEventListener("timeupdate", playShort, false);
  video.addEventListener("loadedmetadata", playShort, false);


  video.load();
  video.play();
}

function playFullVideo() {
  video.removeEventListener("timeupdate", playShort, false)
  video.addEventListener("timeupdate", playFull, false);
  
  video.load();
  video.play();
}

//play short video by default
playShortVideo();


//CLICK events 
var btnshort = $('.shortvideo');
var btnfull = $('.fullvideo');
btnshort.click(function() {

  playShortVideo();
});

btnfull.click(function() {
  playFullVideo();
});

 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <video id="videoElm" autoplay muted controls loop>
    <source src="http://clips.vorwaerts-gmbh.de/VfE_html5.mp4" type="video/webm">
</video>

</div>

<button class="shortvideo">play 2 secs only</a><br>

<button class="fullvideo">loop full video</button> 

0 个答案:

没有答案