爲何要使用video.js?javascript
1. PC端瀏覽器並不支持video直接播放m3u8格式的視頻css
2. 手機端各式各樣的瀏覽器定製的video界面風格不統一,直接寫原生的js控制視頻兼容性較差html
3. video.js解決以上兩個問題,還能夠有各類視頻狀態接口暴露,優化體驗java
核心代碼:git
<!DOCTYPE html> <html> <head> <title>videojs支持hls直播實例</title> <link href="./video.css?v=bcd2ce1385" rel="stylesheet"> </head> <body> <video id="roomVideo" class="video-js vjs-default-skin vjs-big-play-centered" x-webkit-airplay="allow" poster="" webkit-playsinline playsinline x5-video-player-type="h5" x5-video-player-fullscreen="true" preload="auto"> <source src="/chat/playlist.m3u8" type="application/x-mpegURL"> </video> <script src="./video.js?v=fc5104a2ab23"></script> <script src="./videojs-contrib-hls.js?v=c726b94b9923"></script> <script type="text/javascript"> var myPlayer = videojs('roomVideo',{ bigPlayButton : false, textTrackDisplay : false, posterImage: true, errorDisplay : false, controlBar : false },function(){ console.log(this) this.on('loadedmetadata',function(){ console.log('loadedmetadata'); //加載到元數據後開始播放視頻 startVideo(); }) this.on('ended',function(){ console.log('ended') }) this.on('firstplay',function(){ console.log('firstplay') }) this.on('loadstart',function(){ //開始加載 console.log('loadstart') }) this.on('loadeddata',function(){ console.log('loadeddata') }) this.on('seeking',function(){ //正在去拿視頻流的路上 console.log('seeking') }) this.on('seeked',function(){ //已經拿到視頻流,能夠播放 console.log('seeked') }) this.on('waiting',function(){ console.log('waiting') }) this.on('pause',function(){ console.log('pause') }) this.on('play',function(){ console.log('play') }) }); var isVideoBreak; function startVideo() { myPlayer.play(); //微信內全屏支持 document.getElementById('roomVideo').style.width = window.screen.width + "px"; document.getElementById('roomVideo').style.height = window.screen.height + "px"; //判斷開始播放視頻,移除高斯模糊等待層 var isVideoPlaying = setInterval(function(){ var currentTime = myPlayer.currentTime(); if(currentTime > 0){ $('.vjs-poster').remove(); clearInterval(isVideoPlaying); } },200) //判斷視頻是否卡住,卡主3s從新load視頻 var lastTime = -1, tryTimes = 0; clearInterval(isVideoBreak); isVideoBreak = setInterval(function(){ var currentTime = myPlayer.currentTime(); console.log('currentTime'+currentTime+'lastTime'+lastTime); if(currentTime == lastTime){ //此時視頻已卡主3s //設置當前播放時間爲超時時間,此時videojs會在play()後把currentTime設置爲0 myPlayer.currentTime(currentTime+10000); myPlayer.play(); //嘗試5次播放後,如仍未播放成功提示刷新 if(++tryTimes > 5){ alert('您的網速有點慢,刷新下試試'); tryTimes = 0; } }else{ lastTime = currentTime; tryTimes = 0; } },3000) } </script> </body> </html>
源碼請移步github:github
附:瀏覽器
一. 視頻狀態分析:微信
EVENTS
durationchange
ended
firstplay
fullscreenchange
loadedalldata
loadeddata
loadedmetadata
loadstart
pause
play
progress
seeked
seeking
timeupdate
volumechange
waiting
resize inherited
currentTime()能夠用來發輔助判斷視頻播放狀況app
二. 視頻加載優化:
經過不初始化video無用組件的方式,提升video加載速度
var myPlayer = videojs('roomVideo',{ bigPlayButton : false, textTrackDisplay : false, posterImage: true, errorDisplay : false, controlBar : false },function(){});
未簡化以前:
簡化後:
三. 你可能也會遇到的錯誤error
錯誤1:
{code: 4, message: "No compatible source was found for this media."}
解決:去掉video標籤的data-setup="{}", 只保留js的初始配置
錯誤2:
video.js Uncaught TypeError: Cannot read property 'one' of undefined
解決:
var myPlayer = videojs('roomVideo',{ bigPlayButton : false, textTrackDisplay : false, posterImage: false, errorDisplay : false, controlBar : { captionsButton : false, chaptersButton: false, subtitlesButton:false, liveDisplay:false, playbackRateMenuButton:false } },function(){ console.log(this) });
var myPlayer = videojs('roomVideo',{ children : { bigPlayButton : false, textTrackDisplay : false, posterImage: false, errorDisplay : false, controlBar : { captionsButton : false, chaptersButton: false, subtitlesButton:false, liveDisplay:false, playbackRateMenuButton:false } } },function(){ console.log(this) });