你如何检测当HTML5 <视频>元素已经完成播放?
当前回答
你可以添加监听所有视频事件包括结束,加载元数据,时间更新结束函数被调用时,视频结束
$("#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>
其他回答
你可以添加一个以“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>
你可以简单地添加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>
这里有一个完整的例子,我希望它有助于=)。
<!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>
你可以添加监听所有视频事件包括结束,加载元数据,时间更新结束函数被调用时,视频结束
$("#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>
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引用
推荐文章
- 禁用表单自动提交按钮单击
- 链接重新加载当前页面
- 我如何使一个HTML文本框显示空时提示?
- 如何使HTML文本不可选
- 没有定义Electron require()
- JavaScript表单提交-确认或取消提交对话框
- HTML5中的画布宽度和高度
- Ng-app和data-ng-app有什么区别?
- 内容=“X-UA-Compatible IE = 9;IE = 8;IE = 7;IE =边缘”
- 停止/关闭由navigator.mediaDevices.getUserMedia打开的网络摄像头流
- 我如何禁用使用CSS的表单字段?
- 我真的需要将“&”编码为“&”吗?
- 使页脚正确地粘在页底
- 为什么<fieldset>不能是伸缩容器?
- 如果容器元素包含浮动元素,为什么容器元素的高度不增加?