JavaScript/HTML5 暂停后恢复视频播放?

Resume video playback after pause in JavaScript/HTML5?

我正在尝试暂停视频然后继续播放。但我想我不知何故需要清除暂停功能?我试过 myVideo.play() 的迭代,我可以让它播放,但只能播放很短的时间。任何帮助将非常受欢迎。

<!DOCTYPE html>
<html>
<head>
<title> Html5 Video Test </title> 
<meta charset="UTF-8">
</head>

<body> <
<div style="text-align:center">

  <button onclick="playPause()">Play/Pause</button>

  <video id="video1" width="420" autoplay>

    <source src="test.mov" type="video/mov">
  <source src="test.mp4" type="video/mp4">
    Your browser does not support HTML5 video.
  </video>
</div>

<script>


var myVideo = document.getElementById("video1");

myVideo.addEventListener("timeupdate", function(){
    if(this.currentTime >= 1 * 2) {
        this.pause();
    }
});

</script>
</body>
</html>

如果我没理解错的话,您希望您的事件侦听器只触发一次(这样当您下次播放视频时,它不会立即再次暂停)。如果是这样,请试一试:

var myVideo = document.getElementById("video1");

// Give this function a name so we can refer to it later.
function pauseOnce() {
   if (this.currentTime >= 1 * 2) {
       this.pause();

       // Our work is done. Remove the event listener. (We need a reference to
       // the function here.)
       this.removeEventListener("timeupdate", pauseOnce);
   }
}

myVideo.addEventListener("timeupdate", pauseOnce);