你如何检测当HTML5 <视频>元素已经完成播放?
看看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媒体元素(视频/音频)事件文档。
你可以添加一个以“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>
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引用
这里有一个完整的例子,我希望它有助于=)。
<!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>
这里有一个简单的方法,它会在视频结束时触发。
<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'来捕捉这些事件。
你可以添加监听所有视频事件包括结束,加载元数据,时间更新结束函数被调用时,视频结束
$("#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>
你可以简单地添加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>
推荐文章
- 如何在JavaScript中转义单引号(')?
- 如何使用HTML/CSS在文本中插入空格/制表符
- 用背景图像填充SVG路径元素
- 将JSON显示为HTML
- HTML被认为是一种编程语言吗?
- 如何修复favicon.ico错误:“Failed to load resource: net::ERR_EMPTY_RESPONSE”
- 这个CSS是如何产生一个圆的?
- 我如何显示点(“…”)在一个跨度与隐藏溢出?
- 画布在使用CSS时被拉伸,但正常使用“width”/“height”属性
- 如何让一个按钮将我的页面重定向到另一个页面?
- 如何让元素被点击(对于整个文档)?
- Node.js上的html解析器
- 大写还是小写文档类型?
- 样式化HTML电子邮件的最佳实践
- CSS/HTML:什么是使文本斜体的正确方法?