基于vue的video播放器的實(shí)現(xiàn)示例
當(dāng)現(xiàn)有video播放器不能滿足需求時(shí),需要自己對video進(jìn)行封裝。
video事件
- loadstart: 在視頻開始加載時(shí)觸發(fā),給currentTime賦值(歷史播放記錄或0)。
- durationchange: 元信息已載入或已改變,視頻的長度發(fā)生了改變。獲取到視頻長度(duration,并給currentTime賦值(歷史播放記錄或0))。
- playing: 在視頻開始播放時(shí)觸發(fā)(不論是初次播放、在暫停后恢復(fù)、或是在結(jié)束后重新開始)。
- pause: 播放暫停時(shí)觸發(fā)。
- timeupdate: currentTime改變, 更新播放進(jìn)度。處理播放進(jìn)度條
- seeked: 指定播放位置
- waiting: 緩沖
- ended: 播放結(jié)束, 重置狀態(tài)
- WeixinJSBridgeReady: 在微信中使用video,需要監(jiān)聽weixinJSBridgeReady事件, 在回調(diào)函數(shù)里執(zhí)行play()命令。
直播協(xié)議
HLS(HTTP Live Streaming)由Apple提出的直播流協(xié)議。IOS和高版本Android都支持HLS。HLS主要由.m3u8和.ts兩種播放文件。HLS具有高兼容性,高可擴(kuò)展性,但會直播延時(shí)。HLS協(xié)議是將直播流分成一段一段的小段視頻去下載播放的,所以假設(shè)列表里面的包含5個(gè)ts文件,每個(gè)ts文件包含5秒的視頻內(nèi)容,那么整體的延遲就是25秒。
RTMP(Real Time Messaging Protocol)是Macromedia開發(fā)的一套視頻直播協(xié)議,現(xiàn)在屬于Adobe。RTMP基于flash無法在IOS的瀏覽器里播放,但是實(shí)時(shí)性比HLS要好。
HTTP-FLV針對于FLV視頻格式做的直播分發(fā)流,延時(shí)低。但移動端不支持。
結(jié)論:HTTP-FLV延時(shí)低,優(yōu)先使用。若設(shè)備不支持HTTP-FLV,使用flv.js。若設(shè)備不支持flv.js,則使用HLS,但HLS延遲大。
封裝video
/** HTML **/
<div class="video">
<!-- video player -->
<video
class="video__player"
ref="videoPlayer"
preload="auto"
:autoplay="options.autoplay"
:muted="options.muted"
webkit-playsinline="true"
playsinline="true"
x-webkit-airplay="allow"
x5-video-player-type="h5-page"
x5-video-orientation="portraint"
style="object-fit:cover;"
>
<source :src="options.src" />
</video>
<!-- video poster -->
<div
v-show="showPoster"
class="video__poster"
:style="{'background-image': 'url(' + options.pic + ')'}"
></div>
<!-- video loading -->
<div v-show="showLoading" class="video__Loading">
<span class="video__Loading-icon"></span>
</div>
<!-- video pause -->
<div @click="handleVideoPlay" class="video__pause">
<span v-show="!videoPlay" class="video__pause-icon"></span>
</div>
</div>
/** js**/
props: {
options: {
type: Object,
default: () => {}
}
},
data() {
return {
videoPlay: false, // 是否正在播放
videoEnd: false, // 是否播放結(jié)束
showPoster: true, // 是否顯示視屏封面
showLoading: false, // 加載中
currentTime: 0, // 當(dāng)前播放位置
currentVideo: {
duration: this.options.duration
},
}
},
mounted() {
this.videoPlayer = this.$refs.videoPlayer;
this.videoPlayer.currentTime = 0;
this.videoPlayer.addEventListener("loadstart", e => {
this.videoPlayer.currentTime = (this.options.playProcess > 0) ? this.options.playProcess : 0;
});
// 獲取到視頻長度
this.videoPlayer.addEventListener("durationchange", e => {
this.currentVideo.duration = this.videoPlayer.duration;
// 存在播放歷史記錄
this.videoPlayer.currentTime = (this.options.playProcess > 0) ? this.options.playProcess : 0;
});
this.videoPlayer.addEventListener("playing", e => {
this.showPoster = false;
this.videoPlay = true;
this.showLoading = false;
this.videoEnd = false;
});
// 暫停
this.videoPlayer.addEventListener("pause", () => {
this.videoPlay = false;
if (this.videoPlayer.currentTime < 0.1) {
this.showPoster = true;
}
this.showLoading = false;
});
// 播放進(jìn)度更新
this.videoPlayer.addEventListener("timeupdate", e => {
this.currentTime = this.videoPlayer.currentTime;
},
false
);
// 指定播放位置
this.videoPlayer.addEventListener("seeked", e => {
});
// 緩沖
this.videoPlayer.addEventListener("waiting", e => {
this.showLoading = true;
});
// 播放結(jié)束
this.videoPlayer.addEventListener("ended", e => {
// 重置狀態(tài)
this.videoPlay = false;
this.showPoster = true;
this.videoEnd = true;
this.currentTime = this.currentVideo.duration;
});
// 監(jiān)聽weixinJSBridgeReady事件,處理微信不能自動播放。但并不全部適用,加了暫停按鈕,手動播放。
document.addEventListener('WeixinJSBridgeReady', () => {
this.videoPlayer.play();
}, false);
},
methods: {
handleVideoPlay() {
if (this.videoPlayer.paused) { // 播放
if(this.videoEnd) { // 重播
this.currentTime = 0;
this.videoPlayer.currentTime = 0;
}
this.videoPlayer.play();
this.videoPlay = true;
} else { // 暫停
this.videoPlayer.pause();
this.videoPlay = false;
}
}
}
[參考文章]:
到此這篇關(guān)于基于vue的video播放器的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)vue video播放器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue單頁應(yīng)用的內(nèi)存泄露定位和修復(fù)問題小結(jié)
系統(tǒng)進(jìn)程不再用到的內(nèi)存,沒有及時(shí)釋放,就叫做內(nèi)存泄漏(memory leak)。這篇文章主要介紹了vue單頁應(yīng)用的內(nèi)存泄露定位和修復(fù),需要的朋友可以參考下2019-08-08
vue Treeselect下拉樹只能選擇第N級元素實(shí)現(xiàn)代碼
這篇文章主要介紹了vue Treeselect下拉樹只能選擇第N級元素實(shí)現(xiàn)代碼,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
vue+elementUI(el-upload)圖片壓縮,默認(rèn)同比例壓縮操作
這篇文章主要介紹了vue+elementUI(el-upload)圖片壓縮,默認(rèn)同比例壓縮操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Vue.js中computed屬性高效的數(shù)據(jù)處理案例
computed是Vue中一個(gè)計(jì)算屬性,它可以根據(jù)依賴的數(shù)據(jù)動態(tài)計(jì)算出一個(gè)新的值,并將其緩存起來,這篇文章主要給大家介紹了關(guān)于Vue.js中computed屬性高效的數(shù)據(jù)處理的相關(guān)資料,需要的朋友可以參考下2024-09-09
Vue實(shí)現(xiàn)PDF文件預(yù)覽的方法詳解
pdf文件預(yù)覽是開發(fā)業(yè)務(wù)時(shí)常見的一個(gè)交互,在toB項(xiàng)目中是經(jīng)常用到的,對于用戶上傳文件,預(yù)覽文件等操作時(shí)有一個(gè)更好的體驗(yàn),下面我結(jié)合實(shí)際業(yè)務(wù),在vue的基礎(chǔ)上與大家分享這個(gè)實(shí)現(xiàn)方法,需要的朋友可以參考下2023-09-09
el-descriptions引入代碼中l(wèi)abel不生效問題及解決
這篇文章主要介紹了el-descriptions引入代碼中l(wèi)abel不生效問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
vue 輸入電話號碼自動按3-4-4分割功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue 輸入電話號碼自動按3-4-4分割功能的實(shí)現(xiàn)代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Vue.js:使用Vue-Router 2實(shí)現(xiàn)路由功能介紹
本篇文章主要介紹了Vue.js:使用Vue-Router 2實(shí)現(xiàn)路由功能介紹,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02

