检测HTML5视频元素是否正在播放 [英] Detect if HTML5 Video element is playing

查看:2129
本文介绍了检测HTML5视频元素是否正在播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了几个问题,看看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



视频元素没有属性为了获得游戏状态。但是有一个玩的事件会在它开始播放时被触发。

所以解决方案是


    li> decalre一个变量 videoStatus
  1. 为不同的视频事件添加事件处理程序
  2. 更新 videoStatus
  3. strong>使用事件处理程序
  4. 使用 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

  1. decalre one variable videoStatus
  2. add event handlers for different events of video
  3. update videoStatus using the event handlers
  4. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆