video 標籤在微信瀏覽器的問題解決方法

最近作的些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模式

 

x5­video­player­fullscreen全屏設置。筆者還未能領悟這個標籤的用意,在測試過程當中發現,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

在最新的ios微信6.5.3及其以後的版本中,webview默認支持小窗播放,

開發者須要特別注意小窗播放須要前端同時適配iOS10和iOS10如下的低版本

適配建議:須要徹底按照如下代碼設置video標籤纔可同時兼容不一樣的iOS版本

<video webkit-playsinline playsinline> </video>



做者:Lying
連接:https://www.zhihu.com/question/36423771/answer/93953992
來源:知乎
著做權歸做者全部。商業轉載請聯繫做者得到受權,非商業轉載請註明出處。
 
知友說 webkit-playsinline 能夠解決這個問題.可是通過我的以及各位知友的驗證該方法在ios 系統而且是在非微信自帶的瀏覽器的內核中可以解決。Android 然並卵。

 

 

 

 

/****還沒測試的 video.js  聽說,很好用,安卓微信瀏覽器不會全屏播放****/

 

2017-01-20  17:44:22

知友:張豔紅

1,公司網頁要求播放不少小視頻,因而採用了videojs框架,官網連接: Video.js: The Player Framework實測,很好用,並且安卓微信內不會全屏播放
2,效果見我私官網:
3, 另外此框架支持IE8瀏覽器 。


做者:張豔紅
連接:https://www.zhihu.com/question/36423771/answer/108443996
來源:知乎
著做權歸做者全部。商業轉載請聯繫做者得到受權,非商業轉載請註明出處。
 
打算明天測試這個JS 是否如知友所說的那樣好用,若是真是那樣,個人問題就解決了。目前就是安卓手機的視頻全屏的樣子讓人蛋疼!
 
video.js CDN: http://www.bootcdn.cn/video.js/
video.js 教程:http://www.cnblogs.com/afrog/p/4115377.html
 
使用video.js有感而發,使用它主要是爲了兼容iOS與安卓在顯示效果上能夠有比較類似的一面,同時,把視頻或音步的顯示效果自定義一番。上面爲視頻,下面爲音頻:
這個作的還很差,如今是兼容了iOS了,可是安卓上還很差用,安卓上的視頻播放按鈕看不到了,再須要再調整,若是大神知道方法,煩請告我一下,很是感謝。
<!--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();
                    }
                }

 

 
 
參考連接:

移動端HTML5<video>視頻播放優化實踐  :  http://www.xuanfengge.com/html5-video-play.html

html5 audio音頻播放全解析  : http://www.open-open.com/lib/view/open1407401112973.html

相關文章
相關標籤/搜索