如何检测是不是支持 HTML5 自动播放属性?
Posted
技术标签:
【中文标题】如何检测是不是支持 HTML5 自动播放属性?【英文标题】:How do I detect if the HTML5 autoplay attribute is supported?如何检测是否支持 HTML5 自动播放属性? 【发布时间】:2011-10-30 13:06:33 【问题描述】:如何最好地检测浏览器的 html5 视频元素是否支持自动播放?
在当前的 ios Safari 上,例如,autoplay is disabled。
更新:我现在将网页设计为无论是否支持自动播放,它都能正常工作。现在,当页面加载时,会显示一个初始化视频。在 iPad 上,用户会看到一个很大的播放按钮。触发播放后,视频将被隐藏。之后,video player 的播放可以通过 javascript 控制,这正是我真正需要的。
【问题讨论】:
给大家的提示:无论您最终使用哪种方法,请务必在启用和禁用“低功耗模式”的情况下测试您的手机。即使以其他方式支持,这也会阻止自动播放,因此如果播放未按预期开始,您可能需要显示播放按钮。 (例如,在 iOS 上,这是控制表上的电池图标) 【参考方案1】:聚会有点晚了,但公认的解决方案似乎已经过时了:Modernizr 现在已经实现了这个功能,请参阅https://github.com/Modernizr/Modernizr/blob/master/feature-detects/video/autoplay.js
虽然包含与此处发布的其他解决方案类似的 hack,但只要浏览器不公开此功能的可用性,这似乎是目前最好的解决方案。
请注意,这是从 Modernizr 3 开始提供的异步测试,因此您必须使用以下 .on()
语法进行测试:
Modernizr.on('videoautoplay', function(result)
if(result)
alert('video autoplay is supported');
else
alert('video autplay is NOT supported');
);
自己看看:http://codepen.io/anon/pen/VYoWWY?editors=001
以上示例包括带有“videoautplay”功能检测的 Modernizr 3 (http://v3.modernizr.com/download/#-videoautoplay)。
【讨论】:
您能否添加一个小示例,而不仅仅是一个链接(可能会损坏)? 添加了“如何使用”信息、示例代码、codepen 和下载链接;) IE 和 Edge 也有缺点:github.com/Modernizr/Modernizr/issues/1619 @black_falco 不仅是 IE 和 Edge,似乎这个功能在大多数浏览器中仍然很不稳定:HTML5 Video autoplay test does not give reliable results · Issue #1095 · Modernizr/Modernizr 还是个问题。我在 Chrome OSX 上,这很像掷硬币。【参考方案2】:也许这会帮助像我这样的未来谷歌员工 - 此函数创建/删除视频并测试它是否可以自动播放。然后它将全局变量“autoplay”设置为 true 或 false。它测试 MP4 和 webM 的兼容性,但如果需要,添加更多内容并不困难(只需 base 64 编码并附加另一个源)。 “可接受的延迟”变量是设备开始播放测试视频的合理时间。结合有限的带宽/处理,有时使用回退(非自动播放)解决方案是有意义的,即使支持自动播放。我通常使用 100 毫秒。
function detect_autoplay(acceptable_delay)
autoplay = false;
var autoplay_test_content = document.createElement('video');
//create mp4 and webm sources, 5s long
var mp4 = document.createElement('source');
mp4.src = "data:video/mp4;base64,AAAAFGZ0eXBNU05WAAACAE1TTlYAAAOUbW9vdgAAAGxtdmhkAAAAAM9ghv7PYIb+AAACWAAACu8AAQAAAQAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAnh0cmFrAAAAXHRraGQAAAAHz2CG/s9ghv4AAAABAAAAAAAACu8AAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAABAAAAAAFAAAAA4AAAAAAHgbWRpYQAAACBtZGhkAAAAAM9ghv7PYIb+AAALuAAANq8AAAAAAAAAIWhkbHIAAAAAbWhscnZpZGVBVlMgAAAAAAABAB4AAAABl21pbmYAAAAUdm1oZAAAAAAAAAAAAAAAAAAAACRkaW5mAAAAHGRyZWYAAAAAAAAAAQAAAAx1cmwgAAAAAQAAAVdzdGJsAAAAp3N0c2QAAAAAAAAAAQAAAJdhdmMxAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAFAAOABIAAAASAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGP//AAAAEmNvbHJuY2xjAAEAAQABAAAAL2F2Y0MBTUAz/+EAGGdNQDOadCk/LgIgAAADACAAAAMA0eMGVAEABGjuPIAAAAAYc3R0cwAAAAAAAAABAAAADgAAA+gAAAAUc3RzcwAAAAAAAAABAAAAAQAAABxzdHNjAAAAAAAAAAEAAAABAAAADgAAAAEAAABMc3RzegAAAAAAAAAAAAAADgAAAE8AAAAOAAAADQAAAA0AAAANAAAADQAAAA0AAAANAAAADQAAAA0AAAANAAAADQAAAA4AAAAOAAAAFHN0Y28AAAAAAAAAAQAAA7AAAAA0dXVpZFVTTVQh0k/Ou4hpXPrJx0AAAAAcTVREVAABABIAAAAKVcQAAAAAAAEAAAAAAAAAqHV1aWRVU01UIdJPzruIaVz6ycdAAAAAkE1URFQABAAMAAAAC1XEAAACHAAeAAAABBXHAAEAQQBWAFMAIABNAGUAZABpAGEAAAAqAAAAASoOAAEAZABlAHQAZQBjAHQAXwBhAHUAdABvAHAAbABhAHkAAAAyAAAAA1XEAAEAMgAwADAANQBtAGUALwAwADcALwAwADYAMAA2ACAAMwA6ADUAOgAwAAABA21kYXQAAAAYZ01AM5p0KT8uAiAAAAMAIAAAAwDR4wZUAAAABGjuPIAAAAAnZYiAIAAR//eBLT+oL1eA2Nlb/edvwWZflzEVLlhlXtJvSAEGRA3ZAAAACkGaAQCyJ/8AFBAAAAAJQZoCATP/AOmBAAAACUGaAwGz/wDpgAAAAAlBmgQCM/8A6YEAAAAJQZoFArP/AOmBAAAACUGaBgMz/wDpgQAAAAlBmgcDs/8A6YEAAAAJQZoIBDP/AOmAAAAACUGaCQSz/wDpgAAAAAlBmgoFM/8A6YEAAAAJQZoLBbP/AOmAAAAACkGaDAYyJ/8AFBAAAAAKQZoNBrIv/4cMeQ==";
var webm = document.createElement('source');
webm.src = "data:video/webm;base64,GkXfo49CgoR3ZWJtQoeBAUKFgQEYU4BnAQAAAAAAF60RTZt0vE27jFOrhBVJqWZTrIIQA027jFOrhBZUrmtTrIIQbE27jFOrhBFNm3RTrIIXmU27jFOrhBxTu2tTrIIWs+xPvwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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GWgQaDANEBAAEQEAAYABhYL/QACIhgAPuC/rKgnKGWgQfQANEBAAEQEAAYABhYL/QACIhgAPuC/rOgnKGWgQkdANEBAAEQEBRgAGFgv9AAIiGAAPuC/rOgnKGWgQprANEBAAEQEAAYABhYL/QACIhgAPuC/rKgnKGWgQu4ANEBAAEQEAAYABhYL/QACIhgAPuC/rOgnKGWgQ0FANEBAAEQEAAYABhYL/QACIhgAPuC/rOgnKGWgQ5TANEBAAEQEAAYABhYL/QACIhgAPuC/rKgnKGWgQ+gANEBAAEQEAAYABhYL/QACIhgAPuC/rOgnKGWgRDtANEBAAEQEAAYABhYL/QACIhgAPuC/rOgnKGWgRI7ANEBAAEQEAAYABhYL/QACIhgAPuC/rIcU7trQOC7jLOBALeH94EB8YIUzLuNs4IBTbeH94EB8YIUzLuNs4ICm7eH94EB8YIUzLuNs4ID6LeH94EB8YIUzLuNs4IFNbeH94EB8YIUzLuNs4IGg7eH94EB8YIUzLuNs4IH0LeH94EB8YIUzLuNs4IJHbeH94EB8YIUzLuNs4IKa7eH94EB8YIUzLuNs4ILuLeH94EB8YIUzLuNs4INBbeH94EB8YIUzLuNs4IOU7eH94EB8YIUzLuNs4IPoLeH94EB8YIUzLuNs4IQ7beH94EB8YIUzLuNs4ISO7eH94EB8YIUzBFNm3SPTbuMU6uEH0O2dVOsghTM";
//append sources to test video
autoplay_test_content.appendChild(webm);
autoplay_test_content.appendChild(mp4);
//set attributes -needs to be visible or IE squawks, so we move it way outside
autoplay_test_content.id = "base64_test_video";
autoplay_test_content.autoplay = true;
autoplay_test_content.style.position = "fixed";
autoplay_test_content.style.left = "5000px";
//add to DOM
document.getElementsByTagName("body")[0].appendChild(autoplay_test_content);
var base64_test_video = document.getElementById("base64_test_video");
//test for autoplay, 100 ms buffer
setTimeout(function()
if(!base64_test_video.paused)
autoplay = true;
document.getElementsByTagName("body")[0].removeChild(autoplay_test_content);
,acceptable_delay);
detect_autoplay(100);
【讨论】:
这很漂亮:-p 你遇到过需要延迟的情况吗?低带宽应该不是问题,因为您将视频作为数据 URI 加载,但您是否遇到过处理能力对自动播放产生误报的情况? 我实际上遇到了几个使用案例,就像你说它们与处理器相关并且通常与同时运行的多个应用程序等有关。到目前为止,似乎有 200 - 300 毫秒的延迟几乎可以在任何地方工作......谢谢,很高兴它对你有用:-) 你会考虑把代码放到 GitHub 上吗?这样其他人可能会改进它。【参考方案3】:据我所知,Modernizr 和 Dive into HTML5’s detection guide 都不知道如何检测是否支持 autoplay
。 (虽然Modernizr’s “Undetectables” page 也没有列出autoplay
。)
我弹出了a test page,它提醒新创建的<audio>
元素上autoplay
属性的值,iOS 4.3上的Safari和Mac OS X上的Chrome 13都返回false
(如反对 undefined
,这是 IE 6 返回的内容)。
因此,不幸的是,您最好的选择可能是诉诸浏览器检测,并维护您自己的列表,列出哪些浏览器支持autoplay
。
【讨论】:
到目前为止,谢谢,保罗!在将此答案标记为已接受之前,我会稍等片刻。虽然不太可能,但也许有一个解决方案。与此同时,我正在整理清单...... 刚刚提交Modernizr feature request 353。 @feklee:是的,我希望 Stack Overflow 领域的某个人已经遇到并解决了这个问题。不过,这可能是您无法通过 API 进行测试的事情之一。我弹出了a test page,它在新创建的<audio>
元素上提醒autoplay
属性的值,并且iOS Safari 和OS X Chrome 13 都返回false
(而不是undefined
,它是 IE 6 返回的内容)。除非 iOS Safari 提供了一些其他属性告诉你它不支持 autoplay
属性,否则我认为你被卡住了。
我写了一个 Modernizr 测试,它加载一个音频文件,播放它并检查playbackRate
是否被兑现。我对这个解决方案的问题是测试在 IOS 上超时,因为禁止自动播放音频。我一直在寻找gist.github.com/3990089 作为一种可能的解决方法。【参考方案4】:
这是一个替代解决方案,它使用回调和会话存储来测试 HTML5 视频自动播放支持。一旦执行测试并且会话存储存在,则在下一个会话之前不会再次执行检查。这样可以避免每次加载页面时都运行它。它使用 base64 的 1 秒空白视频剪辑来测试自动播放支持。
https://gist.github.com/nathansearles/271870d4100f0f045c5c
// isAutoplaySupported(callback);
// Test if HTML5 video autoplay is supported
isAutoplaySupported = function(callback)
// Is the callback a function?
if (typeof callback !== 'function')
console.log('isAutoplaySupported: Callback must be a function!');
return false;
// Check if sessionStorage exist for autoplaySupported,
// if so we don't need to check for support again
if (!sessionStorage.autoplaySupported)
// Create video element to test autoplay
var video = document.createElement('video');
video.autoplay = true;
video.src = 'data:video/mp4;base64,AAAAIGZ0eXBtcDQyAAAAAG1wNDJtcDQxaXNvbWF2YzEAAATKbW9vdgAAAGxtdmhkAAAAANLEP5XSxD+VAAB1MAAAdU4AAQAAAQAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAACFpb2RzAAAAABCAgIAQAE////9//w6AgIAEAAAAAQAABDV0cmFrAAAAXHRraGQAAAAH0sQ/ldLEP5UAAAABAAAAAAAAdU4AAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAABAAAAAAoAAAAFoAAAAAAAkZWR0cwAAABxlbHN0AAAAAAAAAAEAAHVOAAAH0gABAAAAAAOtbWRpYQAAACBtZGhkAAAAANLEP5XSxD+VAAB1MAAAdU5VxAAAAAAANmhkbHIAAAAAAAAAAHZpZGUAAAAAAAAAAAAAAABMLVNNQVNIIFZpZGVvIEhhbmRsZXIAAAADT21pbmYAAAAUdm1oZAAAAAEAAAAAAAAAAAAAACRkaW5mAAAAHGRyZWYAAAAAAAAAAQAAAAx1cmwgAAAAAQAAAw9zdGJsAAAAwXN0c2QAAAAAAAAAAQAAALFhdmMxAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAoABaABIAAAASAAAAAAAAAABCkFWQyBDb2RpbmcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP//AAAAOGF2Y0MBZAAf/+EAHGdkAB+s2UCgL/lwFqCgoKgAAB9IAAdTAHjBjLABAAVo6+yyLP34+AAAAAATY29scm5jbHgABQA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kAAAAXQZpoSahBaJlMCCv//rUqgAAAAwAAHVEAAAARQZ6GRREsFf8AAAMAAAMADpkAAAAPAZ6ldEFfAAADAAADAA6ZAAAADwGep2pBXwAAAwAAAwAOmAAAABdBmqxJqEFsmUwIK//+tSqAAAADAAAdUAAAABFBnspFFSwV/wAAAwAAAwAOmQAAAA8Bnul0QV8AAAMAAAMADpgAAAAPAZ7rakFfAAADAAADAA6YAAAAF0Ga8EmoQWyZTAgr//61KoAAAAMAAB1RAAAAEUGfDkUVLBX/AAADAAADAA6ZAAAADwGfLXRBXwAAAwAAAwAOmQAAAA8Bny9qQV8AAAMAAAMADpgAAAAXQZs0SahBbJlMCCv//rUqgAAAAwAAHVAAAAARQZ9SRRUsFf8AAAMAAAMADpkAAAAPAZ9xdEFfAAADAAADAA6YAAAADwGfc2pBXwAAAwAAAwAOmAAAABdBm3hJqEFsmUwIK//+tSqAAAADAAAdUQAAABFBn5ZFFSwV/wAAAwAAAwAOmAAAAA8Bn7V0QV8AAAMAAAMADpkAAAAPAZ+3akFfAAADAAADAA6ZAAAAF0GbvEmoQWyZTAgr//61KoAAAAMAAB1QAAAAEUGf2kUVLBX/AAADAAADAA6ZAAAADwGf+XRBXwAAAwAAAwAOmAAAAA8Bn/tqQV8AAAMAAAMADpkAAAAXQZv9SahBbJlMCCv//rUqgAAAAwAAHVE=';
video.load();
video.style.display = 'none';
video.playing = false;
video.play();
// Check if video plays
video.onplay = function()
this.playing = true;
;
// Video has loaded, check autoplay support
video.oncanplay = function()
if (video.playing)
sessionStorage.autoplaySupported = 'true';
callback(true);
else
sessionStorage.autoplaySupported = 'false';
callback(false);
;
else
// We've already tested for support
// use sessionStorage.autoplaySupported
if (sessionStorage.autoplaySupported === 'true')
callback(true);
else
callback(false);
// Usage: isAutoplaySupported(callback);
// Using a callback assures that support
// has been properly checked
isAutoplaySupported(function(supported)
if (supported)
// HTML5 Video Autoplay Supported!
console.log('HTML5 Video Autoplay Supported!');
else
// HTML5 Video Autoplay Not Supported :(
console.log('HTML5 Video Autoplay Not Supported :(');
);
【讨论】:
【参考方案5】:上面的脚本和 Modernizr-detection 不可靠的原因可能是它们没有通过 Apple 所做的所有限制:
视频必须静音 视频必须设置为内嵌播放 视频必须有大小(0x0 像素会失败) 视频必须在视口中可见(无不透明效果,显示:无或可见:隐藏)我想出了一个小 jQuery 插件来检测自动播放。看看是否更可靠:
(function ($)
$.extend(
'canVideoautoplay' : function ( callback )
var testStarted = false;
try
var playsinline = navigator.userAgent.match(/(iPhone|iPod)/g) ? ('playsInline' in document.createElement('video')) : true;
if (!playsinline)
return callback(false);
var src = 'data:video/mp4;base64,AAAAIGZ0eXBpc29tAAACAGlzb21pc28yYXZjMW1wNDEAAAAIZnJlZQAAA7RtZGF0AAACrAYF//+o3EXpvebZSLeWLNgg2SPu73gyNjQgLSBjb3JlIDE1MiByMTkgYmEyNDg5OSAtIEguMjY0L01QRUctNCBBVkMgY29kZWMgLSBDb3B5bGVmdCAyMDAzLTIwMTcgLSBodHRwOi8vd3d3LnZpZGVvbGFuLm9yZy94MjY0Lmh0bWwgLSBvcHRpb25zOiBjYWJhYz0xIHJlZj0zIGRlYmxvY2s9MTowOjAgYW5hbHlzZT0weDM6MHgxMTMgbWU9aGV4IHN1Ym1lPTcgcHN5PTEgcHN5X3JkPTEuMDA6MC4wMCBtaXhlZF9yZWY9MSBtZV9yYW5nZT0xNiBjaHJvbWFfbWU9MSB0cmVsbGlzPTEgOHg4ZGN0PTEgY3FtPTAgZGVhZHpvbmU9MjEsMTEgZmFzdF9wc2tpcD0xIGNocm9tYV9xcF9vZmZzZXQ9LTIgdGhyZWFkcz0zIGxvb2thaGVhZF90aHJlYWRzPTEgc2xpY2VkX3RocmVhZHM9MCBucj0wIGRlY2ltYXRlPTEgaW50ZXJsYWNlZD0wIGJsdXJheV9jb21wYXQ9MCBjb25zdHJhaW5lZF9pbnRyYT0wIGJmcmFtZXM9MyBiX3B5cmFtaWQ9MiBiX2FkYXB0PTEgYl9iaWFzPTA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AABR2bWhkAAAAAQAAAAAAAAAAAAAAJGRpbmYAAAAcZHJlZgAAAAAAAAABAAAADHVybCAAAAABAAAB13N0YmwAAACXc3RzZAAAAAAAAAABAAAAh2F2YzEAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAoABaAEgAAABIAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAY//8AAAAxYXZjQwFkAAv/4QAYZ2QAC6zZQo35IQAAAwAMAAADAs4PFCmWAQAGaOviSyLAAAAAGHN0dHMAAAAAAAAAAQAAABAAAAGAAAAAFHN0c3MAAAAAAAAAAQAAAAEAAACIY3R0cwAAAAAAAAAPAAAAAQAAAwAAAAABAAAHgAAAAAEAAAMAAAAAAQAAAAAAAAABAAABgAAAAAEAAAeAAAAAAQAAAwAAAAABAAAAAAAAAAEAAAGAAAAAAQAAB4AAAAABAAADAAAAAAEAAAAAAAAAAQAAAYAAAAABAAAGAAAAAAIAAAGAAAAAHHN0c2MAAAAAAAAAAQAAAAEAAAAQAAAAAQAAAFRzdHN6AAAAAAAAAAAAAAAQAAAC3gAAAA4AAAAMAAAADAAAAAwAAAAUAAAADQAAAAwAAAAMAAAAEwAAAA0AAAAMAAAADAAAABIAAAANAAAADAAAABRzdGNvAAAAAAAAAAEAAAAwAAAAYnVkdGEAAABabWV0YQAAAAAAAAAhaGRscgAAAAAAAAAAbWRpcmFwcGwAAAAAAAAAAAAAAAAtaWxzdAAAACWpdG9vAAAAHWRhdGEAAAABAAAAAExhdmY1Ny43Ny4xMDA=';
var $video = $('<video src="'+src+'" controls playsinline muted style="position:fixed;z-index:1000;top:0;left:0"></video>');
$('body').prepend($video);
testStarted = true;
$video[0].play();
$video[0].onplay = function()
this.playing = true;
;
$video[0].oncanplay = function()
if ($video[0].playing)
callback(true);
else
callback(false);
$video[0].pause();
$video.remove();
;
catch(e)
if (!testStarted)
callback(false);
);
)(jQuery);
你可以这样使用它:
$(function ()
$.canVideoautoplay(function ( canAutoplay )
alert('Can we autoplay? ' + canAutoplay);
);
);
【讨论】:
【参考方案6】:我知道这是一个视频问题 - 它也适用于音频。
我已经建立了一个音频测试,打算用Modernizr.addTest()
将它加载到Moderizr中。
结果令人惊讶,并且非常依赖于平台和系统状态。原来有几个支持自动播放的 android 浏览器。谁知道呢。
通过查看GitHub,我想 Modernizr 的人会比我更可靠、更优雅地解决这一切——如果他们还没有的话。这似乎归结为时机。你会认为浏览器的家伙会把我们扔在这里。也许他们会。
无论如何,这里是 jsfiddle 链接:Audio().autoplay test。
虽然 jsfiddle 示例中有绒毛代码,但核心如下:
var supportsAutoplay = false; //assume
var audio = new Audio();
var waitTime;
audio.autoplay = true;
audio.volume = 0;
// this will only be triggered if autoplay works
audio.addEventListener('play', function ()
supportsAutoplay = true;
);
audio.src = testSrc; //see if the listener is listening
setTimeout( //wait for listener to run
function()
return supportsAutoplay;
,
waitTime
);
虽然除了上面的那个(Modernizr 的人谈论它)之外,我还没有看到带有setTimeout
的代码,但是这个代码有几个版本。我想我会感谢Peter Coles。也许他的版本不太可能出现时间问题。
【讨论】:
刚刚将pull request 提交给 Modernizr 人员。【参考方案7】:Modernizr 自动播放测试存在一些可靠性问题,如下所示:https://github.com/Modernizr/Modernizr/issues/1095
您可以通过检查视频元素的暂停状态在以编程方式播放后是否发生变化来检测视频自动播放支持。仅此一项就可能在某些移动浏览器中返回误报,因此应添加 Promise 检查以涵盖这些情况。
此方法适用于所有主要浏览器(桌面和移动),但 Android
这里是检测函数:
var supports_video_autoplay = function(callback)
if (typeof callback !== "function") return false;
var v = document.createElement("video");
v.paused = true;
var p = "play" in v && v.play();
callback(!v.paused || ("Promise" in window && p instanceof Promise));
;
用法:
supports_video_autoplay(function(supported)
if (supported)
// video autoplay supported!
else
// no video autoplay support :(
);
现场测试:https://codepen.io/paulorely/pen/QveEGy
【讨论】:
【参考方案8】:这最适合检查自动播放。
var playPromise = document.querySelector('video').play();
// In browsers that don’t yet support this functionality,
// playPromise won’t be defined.
if (playPromise !== undefined)
playPromise.then(function()
// Automatic playback started!
).catch(function(error)
// Automatic playback failed.
// Show a UI element to let the user manually start playback.
);
取自这里。
HTML5 video.play returning Pending promises
【讨论】:
【参考方案9】:也许我回复的有点晚,但我只是测试了一个适合我的解决方案:
var loadStarted = false;
video.onloadstart = function()
loadStarted = true;
;
function checkRunning()
if(!loadStarted) removeVideo();
setTimeout(checkRunning, 1000);
setTimeout 不是很干净,但是整体的解决方法就是这么简单!
【讨论】:
当然,但是添加超时会引入一个明确的阻止程序,您无法对其进行测试。任何使用超时来进行任意“打到头上直到它起作用”修复的解决方案通常都是 hack,而不是解决方案。【参考方案10】:也许你觉得它有用。我将此线程中建议的所有 cmets 组合到此解决方案中: https://github.com/kaufguy/autoplay-detector
这里的演示: https://kaufguy.github.io/autoplay-detector/
我也将其限制为仅限内联播放。
var MUTE_DELAY_PORTION = 25;
var isAutoplaySupported = function (callback, timeout)
var called = false;
if (!callback)
return;
if (!isPlaysinline())
return callback( autoplay: false, muted: false );
checkAutoplay(false, function () testHandler(false); );
setTimeout(function ()
checkAutoplay(true, function () testHandler(true); );
, timeout / MUTE_DELAY_PORTION);
var testHandler = function (mute)
if (!called)
called = true;
callback( autoplay: true, muted: mute );
;
setTimeout(function ()
if (!called)
called = true;
callback( autoplay: false, muted: false );
, timeout);
;
var checkAutoplay = function (mute, callback)
var video = document.createElement('video');
video.ontimeupdate = function ()
if (video.currentTime != 0)
return callback();
;
;
video.autoplay = true;
video.muted = mute;
video.setAttribute('webkit-playsinline', 'webkit-playsinline');
video.setAttribute('playsinline', 'playsinline');
video.src = 'data:audio/mpeg;base64,/+MYxAAAAANIAUAAAASEEB/jwOFM/0MM/90b/+RhST//w4NFwOjf///PZu////9lns5GFDv//l9GlUIEEIAAAgIg8Ir/JGq3/+MYxDsLIj5QMYcoAP0dv9HIjUcH//yYSg+CIbkGP//8w0bLVjUP///3Z0x5QCAv/yLjwtGKTEFNRTMuOTeqqqqqqqqqqqqq/+MYxEkNmdJkUYc4AKqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq';
video.style.display = 'none';
video.load();
video.play();
return video;
;
var isPlaysinline = function ()
return navigator.userAgent.match(/(iPhone|iPod)/g) ? ('playsInline' in document.createElement('video')) : true;
;
isAutoplaySupported(function(autoplayResult)
//do somthing
, 400)
为我工作...
【讨论】:
请在您的帖子中添加问题的答案。当然,您可以链接到您的代码(使用演示环境,这是一个不错的优势!),但仅链接的帖子不被认为是好的答案。以上是关于如何检测是不是支持 HTML5 自动播放属性?的主要内容,如果未能解决你的问题,请参考以下文章