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

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

问题描述

我已经查看了几个问题,以确定是否正在播放 HTML5 元素,但找不到答案.我查看了 W3 文档,它有一个名为播放"的事件,但我似乎无法让它工作.

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.

这是我当前的代码:

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");
    }
  }
}

推荐答案

注意:这个答案是在 2011 年给出的.在继续之前,请检查 HTML5 视频的更新文档.

如果您只想知道视频是否暂停,请使用标志 stream.paused.

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.

所以解决方案是

  1. 声明一个变量 videoStatus
  2. 为视频的不同事件添加事件处理程序
  3. 使用事件处理程序更新 videoStatus
  4. 使用videoStatus来识别视频的状态
  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

此页面将让您更好地了解视频活动.播放此页面上的视频并查看事件是如何触发的.
http://www.w3.org/2010/05/video/mediaevents.html

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 Video 元素是否正在播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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