检测HTML5视频元素是否正在播放 [英] Detect if HTML5 Video element is playing
问题描述
我查看了几个问题,看看HTML5元素是否正在播放,但找不到答案。我看了W3的文档,它有一个名为玩的事件,但我似乎无法得到它的工作。
这是我目前的代码:
var stream = document.getElementsByTagName('video');
函数pauseStream(){
if(stream.playing){
for(var i = 0; i< stream.length; i ++){
stream [I] .pause();
$(body> header)。addClass(paused_note);
$(。paused_note)。text(Stream Paused);
$('。paused_note')。css(opacity,1);
如果您只是想要要知道视频是否已暂停,请使用标记 stream.paused
。
视频元素没有属性为了获得游戏状态。但是有一个玩的事件会在它开始播放时被触发。
所以解决方案是
- 为不同的视频事件添加事件处理程序
- 更新 videoStatus strong>使用事件处理程序
- 使用 videoStatus 识别视频的状态
li> decalre一个变量 videoStatus
此页面可让您更好地了解视频事件。播放此页面上的视频并查看事件是如何触发的。
http:// www.w3.org/2010/05/video/mediaevents.html
I've looked through a couple of questions to find out if an HTML5 element is playing, but can't find the answer. I've looked at the W3 documentation and it has an event named "playing" but I can't seem to get it to work.
This is my current code:
var stream = document.getElementsByTagName('video');
function pauseStream() {
if (stream.playing) {
for (var i = 0; i < stream.length; i++) {
stream[i].pause();
$("body > header").addClass("paused_note");
$(".paused_note").text("Stream Paused");
$('.paused_note').css("opacity", "1");
}
}
}
Note : This answer was given in 2011. Please check the updated documentation on HTML5 video before proceeding.
If you just want to know whether the video is paused, use the flag stream.paused
.
There is no property for video element for getting the playing status. But there is one event "playing" which will be triggered when it starts to play. Event "ended" is triggered when it stops playing.
So the solution is
- decalre one variable videoStatus
- add event handlers for different events of video
- update videoStatus using the event handlers
- use videoStatus to identify the status of the video
This page will give you a better idea about video events. Play the video on this page and see how events are triggered.
http://www.w3.org/2010/05/video/mediaevents.html
这篇关于检测HTML5视频元素是否正在播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!