280

<video>HTML5要素の再生が終了したことをどのように検出しますか?

4

7 に答える 7

344

最初のパラメータとして「終了」を使用してイベントリスナーを追加できます

このような :

<video src="video.ogv" id="myVideo">
  video not supported
</video>

<script type='text/javascript'>
    document.getElementById('myVideo').addEventListener('ended',myHandler,false);
    function myHandler(e) {
        // What you want to do after the event
    }
</script>
于 2010-05-21T09:32:15.480 に答える
138

Opera Dev サイトの「独自のコントロールを展開したい」セクションにあるHTML5 のビデオとオーディオについて知っておくべきすべての投稿をご覧ください。

これは関連するセクションです:

<video src="video.ogv">
     video not supported
</video>

その後、次を使用できます。

<script>
    var video = document.getElementsByTagName('video')[0];

    video.onended = function(e) {
      /*Do things here!*/
    };
</script>

onendedは、すべてのメディア要素の HTML5 標準イベントです。HTML5 メディア要素 (ビデオ/オーディオ) イベントのドキュメントを参照してください。

于 2010-04-30T00:01:27.927 に答える
38

Jクエリ

$("#video1").bind("ended", function() {
   //TO DO: Your code goes here...
});

HTML

<video id="video1" width="420">
  <source src="path/filename.mp4" type="video/mp4">
  Your browser does not support HTML5 video.
</video>

イベント タイプHTML オーディオおよびビデオ DOM リファレンス

于 2013-01-13T06:52:19.033 に答える
8

onended="myFunction()"ビデオタグに追加するだけです。

<video onended="myFunction()">
  ...
  Your browser does not support the video tag.
</video>

<script type='text/javascript'>
  function myFunction(){
    console.log("The End.")
  }
</script>
于 2019-08-23T13:40:47.410 に答える
5

これは、ビデオが終了したときにトリガーされる簡単なアプローチです。

<html>
<body>

    <video id="myVideo" controls="controls">
        <source src="video.mp4" type="video/mp4">
        etc ...
    </video>

</body>
<script type='text/javascript'>

    document.getElementById('myVideo').addEventListener('ended', function(e) {

        alert('The End');

    })

</script>
</html> 

「EventListener」行で、「end」という単語を「pause」または「play」に置き換えて、これらのイベントもキャプチャします。

于 2014-06-19T20:31:01.837 に答える
4

これが完全な例です。お役に立てば幸いです =)。

<!DOCTYPE html> 
<html> 
<body> 

<video id="myVideo" controls="controls">
  <source src="your_video_file.mp4" type="video/mp4">
  <source src="your_video_file.mp4" type="video/ogg">
  Your browser does not support HTML5 video.
</video>

<script type='text/javascript'>
    document.getElementById('myVideo').addEventListener('ended',myHandler,false);
    function myHandler(e) {
        if(!e) { e = window.event; }
        alert("Video Finished");
    }
</script>
</body> 
</html>
于 2014-03-14T08:28:00.287 に答える
0

ビデオの終了時に関数が呼び出されるended, loadedmetadata, timeupdate場所を含むすべてのビデオイベントをリスナーに追加できますended

$("#myVideo").on("ended", function() {
   //TO DO: Your code goes here...
      alert("Video Finished");
});

$("#myVideo").on("loadedmetadata", function() {
      alert("Video loaded");
  this.currentTime = 50;//50 seconds
   //TO DO: Your code goes here...
});


$("#myVideo").on("timeupdate", function() {
  var cTime=this.currentTime;
  if(cTime>0 && cTime % 2 == 0)//Alerts every 2 minutes once
      alert("Video played "+cTime+" minutes");
   //TO DO: Your code goes here...
  var perc=cTime * 100 / this.duration;
  if(perc % 10 == 0)//Alerts when every 10% watched
      alert("Video played "+ perc +"%");
});
<!DOCTYPE html>
<html>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>

<body>

  <video id="myVideo" controls="controls">
    <source src="your_video_file.mp4" type="video/mp4">
      <source src="your_video_file.mp4" type="video/ogg">
        Your browser does not support HTML5 video.
  </video>

</body>

</html>

于 2016-12-08T19:24:54.550 に答える