如何检测 HTML5 的 <video>
元素播放结束?
如何检测 HTML5 的 <video>
元素播放结束?
你可以使用'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>
if(!e) { e = window.event; }
语句是针对早期版本的IE中使用attachEvent
附加的事件处理程序获取最新事件的IE特定代码。由于在这种情况下,处理程序是通过addEventListener
附加的(而且那些早期版本的IE与处理HTML5视频无关),它完全是不必要的,因此我已将其删除。 - Mark Amery请参考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媒体元素(视频/音频)事件文档。
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 音频和视频 DOM 参考
.on()
已经比.bind()
更受青睐。另外,请正确格式化您的代码。 - Mark Ameryonended="myFunction()"
添加到您的视频标签中。<video onended="myFunction()">
...
Your browser does not support the video tag.
</video>
<script type='text/javascript'>
function myFunction(){
console.log("The End.")
}
</script>
<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>
这是一个完整的例子,希望能对你有所帮助 =)。
<!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>
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>