最近作的些web页面,内嵌许多小视频,在ios和安卓手机上播放时,遇到很多问题:javascript
解决办法:css
给video标签加一些属性,调用h5原生video,我写了个例子,加了注释,若是有错误,烦指正,谢谢!html
<video class="video-source" width="100%"
height="240px" /*若是有封面,请设置高度*/
controls /*这个属性规定浏览器为该视频提供播放控件*/ style="object-fit:fill" /*加这个style会让 Android / web 的视频在微信里的视频全屏,若是是在手机上预览,会让视频的封面同视频同样大小*/ webkit-playsinline="true" /*这个属性是ios 10中设置可让视频在小窗内播放,也就是否是全屏播放*/ x-webkit-airplay="true" /*这个属性还不知道做用*/ playsinline="true" /*IOS微信浏览器支持小窗内播放*/ x5-video-player-type="h5" /*启用H5播放器,是wechat安卓版特性*/ x5-video-orientation="h5" /*播放器支付的方向,landscape横屏,portraint竖屏,默认值为竖屏*/ x5-video-player-fullscreen="true" /*全屏设置,设置为 true 是防止横屏*/ preload="auto" /*这个属性规定页面加载完成后载入视频*/ </video>
若是你的video标签也加了上面的属性,那么,你的视频能够在IOS手机上的小窗口播放,同时,视频封面同视频的宽度与高度也保持一致了。前端
通过各类尝试,在iOS下,能够给video添加webkit-playsinline属性,使视频在页面上本来位置播放,但这个属性在安卓上无效。html5
/****华丽丽的分割线,如下内容摘抄自网络,供参考****/java
IOS微信浏览器是Chrome内核,下面大部分属性都支持,安卓微信浏览器是X5内核,一些属性是不支持的,好比能够设置局部播放的属性 playsinline,所以,始终是全屏。android
下面的内容用以解释上面的内容:ios
2017-01-20 17:07:32web
preload="auto" :属性规定在页面加载后载入视频。若是设置了 autoplay 属性,则忽略该属性。浏览器
通常参数可能的值:
· auto - 当页面加载后载入整个视频
· meta - 当页面加载后只载入元数据
· none - 当页面加载后不载入视频
muted:当设置该属性后,它规定视频的音频输出应该被静音
controls="controls" :属性规定浏览器应该为视频提供播放控件。
autoplay="autoplay": 视频自动播放设置,可是有经验的人都应该知道,autoplay标签在手机上不兼容,APP中设置问题致使没法自动播放,不管安卓或IOS。须要模拟自动播放只能经过一些事件触发。
webkit-playsinline playsinline:视频播放时局域播放,不脱离文档流 。可是这个属性比较特别, 须要嵌入网页的APP好比WeChat中UIwebview 的allowsInlineMediaPlayback = YES webview.allowsInlineMediaPlayback = YES,才能生效。换句话说,若是APP不设置,你页面中加了这标签也无效,这也就是为何安卓手机WeChat 播放视频老是全屏,由于APP不支持playsinline,而ISO的WeChat却支持。
这里就要补充下,若是是想作全屏直播或者全屏H5体验的用户,ISO须要设置删除 webkit-playsinline 标签,由于你设置 false 是不支持的 ,安卓则不须要,由于默认全屏。但这时候全屏是有播放控件的,不管你有没有设置control。 作直播的可能用得着播放控件,可是全屏H5是不须要的,那么去除全屏播放时候的控件,须要如下设置:同层播放。
x5-video-player-type:启用同层H5播放器,就是在视频全屏的时候,div能够呈如今视频层上,也是WeChat安卓版特有的属性。同层播放别名也叫作沉浸式播放,播放的时候看似全屏,可是已经除去了control和微信的导航栏,只留下"X"和"<"两键。目前的同层播放器只在Android(包括微信)上生效,暂时不支持iOS。笔者想过为何同层播放只对安卓开放,由于安卓不能像ISO同样局域播放,默认的全屏会使得一些界面操做被阻拦,若是是全屏H5还好,可是作直播的话,诸如弹幕那样的功能就没法实现了,因此这时候同层播放的概念就解决了这个问题。不过笔者在测试的过程当中发现,不一样版本的ISO和安卓效果略有不一样。
x5-video-orientation:声明播放器支持的方向,可选值landscape 横屏, portraint竖屏。默认值portraint。不管是直播仍是全屏H5通常都是竖屏播放,可是这个属性须要x5-video-player-type开启H5模式
x5videoplayerfullscreen:全屏设置。笔者还未能领悟这个标签的用意,在测试过程当中发现,ture和false的设置会致使布局上的不同,如图 Click标签前后的位置。
另外补充下,ISO 微信浏览器是Chrome的内核,相关的属性都支持,也是为何X5同层播放不支持的缘由。安卓微信浏览器是X5内核,一些属性标签好比playsinline就不支持,因此始终全屏。
/****貌似到这里,了解了为何安卓手机的全屏视频了****/
2017-01-20 17:13:55
还有个问题,在Android的微信里面,就算加上了上面的属性,还会出现上下有黑边,不能全屏的问题。
解决办法:给video加上object-fit: fill;的style属性。
/****关于微信内置浏览器不能小窗口播放视频的问题-知乎答****/
2017-01-20 17:40:38
开发者须要特别注意小窗播放须要前端同时适配iOS10和iOS10如下的低版本
适配建议:须要彻底按照如下代码设置video标签才可同时兼容不一样的iOS版本
<video webkit-playsinline playsinline> </video>
/****还没测试的 video.js 听说,很好用,安卓微信浏览器不会全屏播放****/
2017-01-20 17:44:22
知友:张艳红
<!--video.js--> <link href="http://vjs.zencdn.net/5.8.8/video-js.css" rel="stylesheet"> <script src="http://vjs.zencdn.net/ie8/1.1.2/videojs-ie8.min.js"></script> <script src="http://vjs.zencdn.net/5.8.8/video.js"></script>
<script type="text/javascript"> var source = $('<source></source>'); //视频url地址 var audioPlayImg = "../img/doctor/play-audio.png"; //播放按钮 var pauseImg = "../img/doctor/pause.png"; //暂停按钮 (function(window, document) { searchVideo(); //获取要操做的元素 var video = document.getElementById("video"); var audio = document.getElementById('audio'); var videoControls = document.getElementById("videoControls"); var audioControls = document.getElementById("audioControls"); var videoContainer = document.getElementById("videoContainer"); var audioControls = document.getElementById("audioControls"); var controls = document.getElementById("video_controls"); var playBtn = document.getElementById("playBtn"); var audioBtn = document.getElementById("audioPlayBtn"); var fullScreenFlag = false; var progressFlag; // 音频: 建立咱们的操做对象,咱们的全部操做都在这个对象上。 var audioPlayr = { init: function() { var that = this; //删除音频的控制按钮属性 audio.removeAttribute("controls"); bindEvent(audio, "loadeddata", audioPlayr.initControls); audioPlayr.operateControls(); }, initControls: function() { audioPlayr.showHideControls(); }, showHideControls: function() { bindEvent(audio, "mouseover", showAudioControls); bindEvent(audioControls, "mouseover", showAudioControls); bindEvent(audio, "mouseout", hideAudioControls); bindEvent(audioControls, "mouseout", hideAudioControls); }, operateControls: function() { bindEvent(audioBtn, "click", audioPlay); bindEvent(audio, "click", audioPlay); } } audioPlayr.init(); function showAudioControls() { videoControls.style.opacity = 1; } function hideAudioControls() { videoControls.style.opacity = 1; } function audioPlay() { if(audio.paused || audio.ended) { if(audio.ended) { audio.currentTime = 0; } audio.play(); audioBtn.setAttribute('src', pauseImg); } else { audio.pause(); audioBtn.setAttribute('src', audioPlayImg); clearInterval(progressFlag); } } // 视频: 建立咱们的操做对象,咱们的全部操做都在这个对象上。 var videoPlayer = { init: function() { var that = this; //删除视频的播放按钮属性 video.removeAttribute("controls"); //若是当前的数据是可用的,为视频绑定事件 bindEvent(video, "loadeddata", videoPlayer.initControls); videoPlayer.operateControls(); }, initControls: function() { videoPlayer.showHideControls(); }, showHideControls: function() { bindEvent(video, "mouseover", showControls); bindEvent(videoControls, "mouseover", showControls); bindEvent(video, "mouseout", hideControls); bindEvent(videoControls, "mouseout", hideControls); }, operateControls: function() { bindEvent(playBtn, "click", isIOSOrAndroid); bindEvent(video, "click", isIOSOrAndroid); } } videoPlayer.init(); // 原生的JavaScript事件绑定函数 function bindEvent(ele, eventName, func) { if(window.addEventListener) { ele.addEventListener(eventName, func); } else { ele.attachEvent('on' + eventName, func); } } // 显示video的控制面板 function showControls() { // 为了让控制面板一直出现,我把videoControls.style.opacity的值改成1 videoControls.style.opacity = 1; } // 隐藏video的控制面板 function hideControls() { videoControls.style.opacity = 1; } //判断当前系统是iOS系统仍是安卓系统,由于iOS微信浏览器会加载完视频后自动添加一个播放按钮。而咱们的播放按钮是自定义的 function isIOSOrAndroid() { var ua = navigator.userAgent.toLowerCase(); if(ua.match(/iPhone\sOS/i) == "iphone os") { iOSPlay(); } else if(ua.match(/Android/i) == "android") { androidPlay(); } } // 控制video的播放 当前操做是在iOS系统下操做 function iOSPlay() { $('.videoPlayer').css('margin-bottom', '0'); if(video.paused || video.ended) { if(video.ended) { video.currentTime = 0; } video.play(); playBtn.setAttribute('src', pauseImg); } else { video.pause(); playBtn.setAttribute('src', audioPlayImg); clearInterval(progressFlag); } } // 控制video的播放 当前操做是在安卓系统下操做 function androidPlay() { if(video.paused || video.ended) { if(video.ended) { video.currentTime = 0; } video.play(); playBtn.setAttribute('src', audioPlayImg); } else { video.pause(); playBtn.setAttribute('src', audioPlayImg); clearInterval(progressFlag); } } }(this, document)) </script>
HTML:
<div class="video-play-content"> <!--视频--> <div class="videoPlayer video-js" id="videoContainer" style="height: 200px;margin-bottom: 15px;"> <video id="video" width="100%" height="200" preload controls style="object-fit: fill" webkit-playsinline="true" x-webkit-airplay="true" playsinline="true" x5-video-player-type="h5" x5-video-player-fullscreen="true"> </video> <div id="videoControls"> <div class="video-box"> <img id="playBtn" src="" /> </div> </div> </div> <!--音频--> <div id="audioControls"> <audio id="audio" width="100%" height="50" preload controls> <source src="../video/wssf.mp3" type="audio/mp3"></source> </audio> <div id="audioControls"> <div class="audio-box"> <img id="audioPlayBtn" src="../img/doctor/play-audio.png" style="-webkit-appearance:none;" /> <div class="audio-detail"> <div class="audio-title">骨髓灰质炎疫苗选择</div> <div class="audio-size"><span class="audio-time">24:26</span><span class="audio-size">11.44</span>MB</div> </div> </div> </div> </div> </div>
CSS:
body{ color: #555; font-family:Helvetica; margin: 15px; /*去掉默认外观*/ -webkit-appearance:none; /*长按不触发系统菜单*/ -webkit-touch-callout: none; /*禁止选中文字*/ -webkit-user-select:none; } @font-face { font-family: 'MicrosoftYaHei'; src: url('MicrosoftYaHei.eot'); /* IE9 Compat Modes */ src: url('MicrosoftYaHei.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */ url('MicrosoftYaHei.woff') format('woff'), /* Modern Browsers */ url('MicrosoftYaHei.ttf') format('truetype'), /* Safari, Android, iOS */ url('MicrosoftYaHei.svg#MicrosoftYaHei') format('svg'); /* Legacy iOS */ } html{ font-size:13px; height: 100%; -webkit-tap-highlight-color: transparent; } @media screen and (min-width:321px) and (max-width:375px){ html{font-size:11px} .logo-box{ position: absolute; top: -56px; left: 50%; transform: translateX(-50%); } } @media screen and (min-width:376px) and (max-width:414px){ html{font-size:12px} .logo-box{ position: absolute; top: -56px; left: 50%; transform: translateX(-50%); } } @media screen and (min-width:415px) and (max-width:639px){html{font-size:15px}} @media screen and (min-width:640px) and (max-width:719px){html{font-size:20px}} @media screen and (min-width:720px) and (max-width:749px){html{font-size:22.5px}} @media screen and (min-width:750px) and (max-width:799px){html{font-size:23.5px}} @media screen and (min-width:800px){html{font-size:25px}} .main{ margin-bottom: 40px; } .video-js { display: block; vertical-align: top; box-sizing: border-box; color: #fff; background-color: #000; position: relative; padding: 0; font-size: 12px; line-height: 1; font-weight: normal; font-style: normal; font-family: Arial, Helvetica, sans-serif; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; height: 200px; } .videoPlayer{ width: 100%; position: relative; margin-bottom: 15px; /*background: #555;*/ } #video{ android:layout_margin="0dp"; android:padding="0dp"; } #audioControls,.video-explain-3{ margin-bottom: 15px; } #playBtn{ width: 5.5em; position: absolute; top: 72px; left: 50%; transform: translateX(-50%); z-index: 9; cursor: pointer; } .audio-box{ width: 100%; margin: auto; height: 80px; background: #e2e2e2; position: relative; display: flex; justify-content: space-around; align-items: center; } #audioPlayBtn { width: 5em; position: absolute; top: 10.5%; left: 6%; z-index: 9; cursor: pointer; } .vaccine{ width: 100%; margin: 15px 0; } .video-info{ margin-bottom: 20px; color: #999; } .audio-time{ margin-right: 10px; }
修改一下判断浏览系统的方法:
function isIOSOrAndroid() { var browser = { versions: function() { var u = navigator.userAgent, app = navigator.appVersion; return { //移动终端浏览器版本信息 trident: u.indexOf('Trident') > -1, //IE内核 presto: u.indexOf('Presto') > -1, //opera内核 webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核 gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核 mobile: !!u.match(/AppleWebKit.*Mobile.*/) || !!u.match(/AppleWebKit/), //是否为移动终端 ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端 android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器 iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者QQHD浏览器 iPad: u.indexOf('iPad') > -1, //是否iPad webApp: u.indexOf('Safari') == -1 //是否web应该程序,没有头部与底部 }; }(), language: (navigator.browserLanguage || navigator.language).toLowerCase() } if(browser.versions.ios || browser.versions.iPhone || browser.versions.iPad) { $('.videoPlayer').css('margin-bottom', '15px'); iOSPlay(); } else if(browser.versions.android) { androidPlay(); } }