代码之家  ›  专栏  ›  技术社区  ›  Brent

检测HTML5视频何时结束

  •  225
  • Brent  · 技术社区  · 14 年前

    你如何检测一个HTML5 <video> 元素已完成播放?

    6 回复  |  直到 6 年前
        1
  •  269
  •   Mark Amery Harley Holcombe    9 年前

    可以添加一个以“ended”作为第一个参数的事件侦听器

    这样地:

    <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>
    
        2
  •  129
  •   Mathias Rechtzigel    10 年前

    看看这个 Everything You Need to Know About HTML5 Video and Audio 在OperaDev站点的“我想滚动我自己的控件”部分下发布。

    这是相关章节:

    <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 文档。

        3
  •  35
  •   d1jhoni1b    9 年前

    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

        4
  •  4
  •   Oscar_Wroclaski    10 年前

    这是一个完整的例子,我希望它有助于=。

    <!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>
    
        5
  •  4
  •   posit labs    8 年前

    下面是一个简单的方法,当视频结束时触发。

    <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”行中,用“pause”或“play”替换“ended”以捕获这些事件。

        6
  •  0
  •   jafarbtech    8 年前

    您可以添加监听器所有视频事件 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>