简体   繁体   English

确定Chrome是否阻止了HTML5视频自动播放?

[英]Determine if Chrome has blocked HTML5 video autoplay?

As of Chrome 46, new background tabs will no longer autoplay audio or video. 从Chrome 46开始,新的背景标签将不再自动播放音频或视频。 How can an application tell if its autoplay has been blocked? 应用程序如何知道其自动播放是否已被阻止?

Some ideas: 一些想法:

  • UA sniff for Chrome 46+ 适用于Chrome 46+的UA嗅探器
  • check if tab is visible with Visibility API 检查标签在Visibility API中是否可见
  • check if new tab with History API 检查带有History API的新标签页
  • check if video is actually playing 检查视频是否正在播放

Something hacky like this will probably work, but I would really like to know if there's a better way: 像这样的hacky可能会起作用,但是我真的很想知道是否有更好的方法:

function isAutoplayBlocked() {
  return isChrome46 && isNewTab && isHiddenTab;
}

Firstly on mobile, autoplay is completely blocked. 首先,在移动设备上,自动播放被完全阻止。

The best way to see if your video is blocked is to look for the media events that indicate that the element has started playing such as ontimeupdate and your element has autoplay attribute attached. 查看视频是否被阻止的最佳方法是查找媒体事件,这些事件表明元素已开始播放,例如ontimeupdate且元素具有自动播放属性。 If there are no updates and no user interactions then you can assume that autoplay has been suspended. 如果没有更新,也没有用户交互,则可以假定自动播放已暂停。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM