检测HTML5视频何时完成

时间:2010-04-29 23:52:01

标签: html5 html5-video

如何检测HTML5 <video>元素何时播放完毕?

7 个答案:

答案 0 :(得分:274)

您可以添加一个“已结束”的事件监听器作为第一个参数

像这样:

<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>

答案 1 :(得分:131)

在Opera Dev网站的“我想要自己动手控制”部分下看看 Everything You Need to Know About HTML5 Video and Audio 帖子。

这是相关部分:

<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 media element (video/audio) events文档。

答案 2 :(得分:35)

JQUERY

$("#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 Audio and Video DOM Reference

答案 3 :(得分: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>

答案 4 :(得分:4)

这是一种在视频结束时触发的简单方法。

<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'行中,将'ended'替换为'pause'或'play'来捕获这些事件。

答案 5 :(得分:0)

您可以添加监听器所有视频事件,其中包含ended, loadedmetadata, timeupdate视频结束时调用ended函数的内容

&#13;
&#13;
$("#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 +"%");
});
&#13;
<!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>
&#13;
&#13;
&#13;

答案 6 :(得分:0)

您只需将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>