如何检测是否支持 HTML5 自动播放属性?
如何最好地检测浏览器的 HTML5 视频元素是否支持自动播放?
例如,在当前的 iOS Safari 上,自动播放是已禁用。
更新:我现在设计的网页的方式使其无论是否支持自动播放都可以正常工作。现在,当页面加载时,会显示一个初始化视频。在 iPad 上,用户会看到一个大的播放按钮。一旦触发播放,视频就会隐藏。之后,可以通过 JavaScript 控制视频播放器的播放,这就是我的目的实际需要。
How do I best detect whether a browser's HTML5 video element supports autoplay?
On current iOS Safari, for example, autoplay is disabled.
Update: I now designed the web page in such a way that it works irrespective of whether autoplay is supported. Now when the page is loaded an initialization video is shown. On an iPad, the user is presented with a big play button. Once playback has been triggered, the video is hidden. Afterwards, playback of the video player can be controlled from JavaScript, which is what I actually need.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
聚会有点晚了,但接受的解决方案似乎已经过时了:Modernizr 现在已经实现了此功能,请参阅 https://github.com/Modernizr/Modernizr/blob/master/feature-detects/video/autoplay.js
包含与发布的其他解决方案类似的黑客攻击不过,只要浏览器不公开此功能的可用性,这似乎是目前最好的解决方案。
请注意,这是自 Modernizr 3 起可用的异步测试,因此您必须使用以下
.on()
语法进行测试:亲自查看:http://codepen.io/anon/pen/VYoWWY?editors=001
上面的示例包括 Modernizr 3 以及“videoautplay”功能检测 (http://v3.modernizr.com/download/#-videoautoplay)。
A little late to the party, but the accepted solution seems outdated: Modernizr now has implemented this feature, see https://github.com/Modernizr/Modernizr/blob/master/feature-detects/video/autoplay.js
Contains similar hacks to the other solutions posted here though, but as long as browsers don't expose availabilty of this feature, this seems to be the best solution for now.
Note that this an async test available since Modernizr 3, so you have to use the following
.on()
syntax for your test:See for yourself: http://codepen.io/anon/pen/VYoWWY?editors=001
The above sample includes Modernizr 3 with the 'videoautplay' feature detection (http://v3.modernizr.com/download/#-videoautoplay).
也许这会帮助像我这样的未来谷歌人 -
此函数创建/删除视频并测试它是否可以自动播放。然后它将全局变量“autoplay”设置为 true 或 false。它测试 MP4 和 webM 兼容性,但如果需要的话添加更多内容并不困难(只需进行 Base 64 编码并附加另一个源)。
“可接受的延迟”变量是设备开始播放测试视频所需的合理时间。由于带宽/处理能力有限,有时即使支持自动播放,使用后备(非自动播放)解决方案也是有意义的。我一般用100ms。
Maybe this will help future googlers like myself -
this function creates/removes a video and tests to see if it can autoplay. It then sets a global variable "autoplay" to true or false. It tests for MP4 and webM compatibility, but adding more wouldn't be difficult if need be (just base 64 encode and append another source).
The "acceptable delay" variable is how long it should reasonably take the device to start playing the test videos. With a combination of limited bandwidth/processing sometimes it makes sense to use the fallback (non autoplay) solution, even if autoplay is supported. I usually use 100ms.
据我所知, Modernizr 和 深入研究 HTML5 的检测指南了解如何检测是否支持
自动播放
。 (尽管Modernizr 的“UnDetectables”页面未列出自动播放
)我弹出了 一个测试页面,它会提醒新创建的
元素上的
autoplay
属性的值,并且 Safari 上Mac OS X 上的 iOS 4.3 和 Chrome 13 返回false
(与 IE 6 返回的undefined
相反)。因此,不幸的是,您最好的选择可能是求助于浏览器检测,并维护您自己的支持
自动播放
的浏览器列表。As far as I can tell, neither Modernizr nor Dive into HTML5’s detection guide know how to detect whether
autoplay
is supported. (Although Modernizr’s “Undetectables” page doesn’t listautoplay
either.)I’ve popped up a test page which alerts the value of the
autoplay
property on a newly-created<audio>
element, and both Safari on iOS 4.3 and Chrome 13 on Mac OS X returnfalse
(as opposed toundefined
, which is what IE 6 returns).So, unfortunately, your best bet might be to resort to browser detection, and maintain your own list of which browsers support
autoplay
.这是一种替代解决方案,它使用回调和会话存储来测试 HTML5 视频自动播放支持。一旦执行测试并且会话存储存在,在下一个会话之前不会再次执行检查。这样就可以避免每次加载页面时都运行它。它使用 base64 的 1 秒空白视频剪辑来测试自动播放支持。
https://gist.github.com/nathansearles/271870d4100f0f045c5c
Here's an alternative solution that uses a callback and session storage to test for HTML5 video autoplay support. Once the test is preformed and the session storage exist the check isn't preformed again until the next session. This saves from running this every time a page loads. It uses a base64'd 1 second blank video clip to test for autoplay support.
https://gist.github.com/nathansearles/271870d4100f0f045c5c
我意识到这是一个视频问题 - 它也适用于音频。
我构建了一个音频测试,目的是使用
Modernizr.addTest()
将其加载到 Moderizr 中。结果令人惊讶,并且非常依赖于平台和系统状态。事实证明,有几种 Android 浏览器支持自动播放。谁知道呢。
通过查看 GitHub,我想 Modernizr 的人会更可靠、更优雅地解决这一切比我有——如果他们还没有的话。这似乎归结为时间问题。你可能会认为浏览器开发人员会在这里向我们扔一根骨头。也许他们会的。
无论如何,这是 jsfiddle 链接:Audio().autoplay test。
虽然 jsfiddle 示例中有一些无用的代码,但核心是:
虽然除了上面的那个(Modernizr 的人谈论它)之外,我还没有见过带有
setTimeout
的代码,但有几个版本周围的代码。我想我会归功于Peter Coles。也许他的版本不太可能遇到时间问题。I realize this is a video question - and it applies to audio too.
I have built an audio test with the intention of loading it into Moderizr with
Modernizr.addTest()
.The results are surprising and very dependent on platform and system state. Turns out there are several Android browsers that support autoplay. Who knew.
From looking at GitHub, I imagine the Modernizr guys will figure all this out more reliably and elegantly than I have - if they haven't already. It seems to boil down to timing. You'd think the browsers guys would throw us a bone here. Maybe they will.
Anyway, here is the jsfiddle link: Audio().autoplay test.
While there is fluff code in the jsfiddle example, here's the core:
While I have not seen one with a
setTimeout
other than the one above (the Modernizr guys talk about it), there are several versions of this code around. I guess I'll credit Peter Coles. Maybe his version is less likely to suffer from timing issues.上述脚本和 Modernizr 检测不可靠的原因可能是它们没有通过 Apple 制定的所有限制:
我想出了一个小的 jQuery 插件来检测自动播放。看看它是否更可靠:
你可以这样使用它:
The reason why the script above and the Modernizr-detection isn't reliable might be that they are not passing all the restrictions that Apple made:
I came up with tiny jQuery plugin to detect autoplay. See if it is more reliable:
You can use it like this:
Modernizr 自动播放测试存在一些可靠性问题,如下所示: https://github.com/Modernizr/ Modernizr/issues/1095
您可以通过检查视频元素的暂停状态在以编程方式播放后是否发生变化来检测视频自动播放支持。仅此一项就可能在某些移动浏览器中返回漏报,因此应添加 Promise 检查以覆盖这些情况。
此方法适用于所有主要浏览器(桌面和移动设备),但 Android <= 4.0 和 Windows Phone 除外,在这些浏览器中它会返回漏报。
这是检测功能:
用法:
实时测试: https://codepen.io/paulorely/pen/QveEGy
Modernizr autoplay test has some reliability issues, as pointed here: https://github.com/Modernizr/Modernizr/issues/1095
You can detect video autoplay support by checking if the paused status of a video element changes after it programmatically plays. This alone can return false negatives in some mobile browsers, so a Promise check should be added in order to cover these.
This method works in all major browsers (desktop and mobile), except for Android <= 4.0 and Windows Phone, where it returns a false negative.
Here is the detect function:
Usage:
Live test: https://codepen.io/paulorely/pen/QveEGy
这最适合检查自动播放。
取自这里。
HTML5 video.play 返回待处理的承诺
This works best to check for autoplay.
Taken from here.
HTML5 video.play returning Pending promises
也许我回复有点晚了,但我只是测试了一个对我来说效果很好的解决方案:
setTimeout 不是很干净,但整体解决方案非常简单!
Maybe I respond a little bit late but I just test a solution which work fine for me :
The setTimeout is not very clean, but the overall solution is super simple like that !
也许你觉得它很有用。我将此线程中建议的所有评论合并到此解决方案中:
https://github.com/kaufguy/autoplay-detector
此处演示:
我也
将其限制为仅限内联播放。
对我有用...
Maybe you find it useful. I combined all the comments suggested in this thread into this solution:
https://github.com/kaufguy/autoplay-detector
Demo here:
https://kaufguy.github.io/autoplay-detector/
I also restricted it to inline play only.
Works for me...