Html 如何检查浏览器是否可以通过 html5 视频标签播放 mp4?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3572113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if the browser can play mp4 via html5 video tag?
提问by Coepr
How to check if the browser can play mp4 via html5 video tag?
如何检查浏览器是否可以通过 html5 视频标签播放 mp4?
回答by Alex Polo
This might help you:
这可能会帮助您:
<script type="text/javascript">'.
var canPlay = false;
var v = document.createElement('video');
if(v.canPlayType && v.canPlayType('video/mp4').replace(/no/, '')) {
canPlay = true;
}
alert(canPlay);
</script>
回答by badfur
Alex Polo's reply is not bad but incomplete try this to check if the codec is supported too:
Alex Polo 的回复还不错但不完整,请尝试检查是否也支持编解码器:
var mp4Supported = (!!document.createElement('video').canPlayType('video/mp4; codecs=avc1.42E01E,mp4a.40.2'));
Likewise for ogg, webm and so on ... Works with audio too :)
同样适用于 ogg、webm 等......也适用于音频:)
回答by Justin808
回答by Cornac
I have to check if the html5 video is displayed, to hide my personal button to play and turn audio off in ie7 and ie8. My solution is shown below.
我必须检查是否显示了 html5 视频,以隐藏我的个人按钮以在 ie7 和 ie8 中播放和关闭音频。我的解决方案如下所示。
My html:
我的html:
<div id="contenitore_video" style="position:absolute;top:0;left:0;">
<video id="cont_video" autoplay onFocus="this.blur();" width="100%" height="100%" >
<source src="video/xxx.mp4" type="video/mp4" />
<source src="video/xxx.theora.ogv" type="video/ogg" />
<div id="sfondo_ridimensionato" >
<img src="img/sfondo_home1.jpg" >
</div>
</video>
</div>
...
<div id="controlli_video" style="position:absolute; top:10px;right:25px; height:50px; text-align:right;">
<a class="video_play" onFocus="this.blur();" style="display:none;" href="javascript:void(0)" onClick="controlla_video(1)">Play</a> ...
</div
My JS ready:
我的 JS 准备好了:
$(document).ready(function(){
//controllo se il video funziona o si vede il video alternativo
// var numero = $('#sfondo_ridimensionato:hidden').length;
// alert(numero);
if($('#sfondo_ridimensionato:hidden').length == 0){
$('#controlli_video').hide();
}
}
回答by Ender Bonnet
I made a function to validate if your browser is able to play audio or video element
我做了一个功能来验证您的浏览器是否能够播放音频或视频元素
const checkMedia = mediaType => {
let canPlay = false
const mediaFormat = {
audio: 'audio/mp3',
video: 'video/mp4'
}
let media = document.createElement(mediaType)
if (
media.canPlayType &&
media.canPlayType(mediaFormat[mediaType]).replace(/no/, '')
) {
canPlay = true
}
return canPlay
}