Vue使用video標(biāo)簽實(shí)現(xiàn)視頻播放
本文項(xiàng)目為大家分享了Vue使用video標(biāo)簽實(shí)現(xiàn)視頻播放的具體代碼,供大家參考,具體內(nèi)容如下
項(xiàng)目需求:動(dòng)態(tài)顯示視頻滾動(dòng)條、禁止視頻下載、播放時(shí)每5s更新當(dāng)前時(shí)長(zhǎng)、每10分鐘暫停視頻顯示提示。
之前做視頻播放時(shí)基本都是使用 vue-video-player 組件,其原因?yàn)?封裝功能齊全、播放源兼容性好等。
通過(guò)這次項(xiàng)目需求,也深入的學(xué)習(xí)了 video 標(biāo)簽的屬性及方法。具體在這里跟大家分享一下。
具體使用方法如下
<template>
<!-- Video組件 -->
<div id="common-video" class="h-100">
<div :class="{ isShow: control }" class="h-100">
<video
ref="myVideo"
:poster="poster"
:src="src"
:controls="controls"
oncontextmenu="return false"
@timeupdate="timeupdate"
controlslist="nodownload"
class="video-box"
></video>
<img
src="@/assets/images/playbtn.png"
alt=""
@click="operateVideo"
class="pointer operate-btn"
:class="{ 'fade-out': videoState }"
/>
</div>
</div>
</template>
<script>
export default {
name: "CommonVideo",
data() {
return {
videoState: false, // 視頻播放狀態(tài)
// 學(xué)時(shí)
studyTime: {
currentTime: 0, // 當(dāng)前已學(xué)時(shí)長(zhǎng)
duration: 0 // 總時(shí)長(zhǎng)
},
timer: {}, // 定時(shí)器
pauseTimer: {} // 暫停定時(shí)器
};
},
/**
* @param poster 展示圖
* @param src 視頻資源
* @param controls 是否顯示控件
* @param control 控件控制
* @param videoData 視頻基礎(chǔ)數(shù)據(jù)
*/
props: {
poster: {
type: String,
required: false,
default: ""
},
src: {
type: String,
required: true
},
controls: {
type: Boolean,
required: false,
default: true
},
control: {
type: Boolean,
required: false,
default: false
},
videoData: {
type: Object,
required: true
}
},
mounted() {
// 監(jiān)聽視頻播放
this.$refs.myVideo.addEventListener("play", () => {
console.log("video is playing");
this.openTimer();
});
// 監(jiān)聽視頻暫停
this.$refs.myVideo.addEventListener("pause", () => {
console.log("video is stop");
this.closeTimer();
});
},
methods: {
// 開啟定時(shí)器
openTimer() {
this.timer = setInterval(() => {
this.$emit("videoStudyTime", this.studyTime);
}, 5000);
},
// 關(guān)閉定時(shí)器
closeTimer() {
clearInterval(this.timer);
this.$emit("videoStudyTime", this.studyTime);
},
// 開啟暫停定時(shí)器
openPauseTimer() {
this.pauseTimer = setInterval(() => {
this.hintOperate();
}, 600000);
},
// 關(guān)閉暫停定時(shí)器
closePauseTimer() {
clearInterval(this.pauseTimer);
},
// 提示操作
hintOperate() {
this.operateVideo();
this.$alert("請(qǐng)點(diǎn)擊確認(rèn)繼續(xù)學(xué)習(xí)", "提示", {
confirmButtonText: "確定",
confirmButtonClass: "hint-btn",
showClose: false,
callback: action => {
this.$refs.myVideo.currentTime = this.videoData.currentTime;
this.operateVideo();
this.openPauseTimer();
}
});
},
// 獲取當(dāng)前播放位置
timeupdate(e) {
this.studyTime.currentTime = e.target.currentTime;
this.studyTime.duration = e.target.duration ? e.target.duration : 0;
},
// 操作視頻播放、暫停
operateVideo() {
if (!this.src) {
this.$message({ message: "暫無(wú)視頻資源,請(qǐng)查看其他視頻!" });
return false;
}
if (this.$refs.myVideo.paused) {
this.$refs.myVideo.play();
this.videoState = true;
} else {
this.$refs.myVideo.pause();
this.videoState = false;
}
}
},
watch: {
// 監(jiān)聽操作
videoData(val, oldVal) {
const { currentTime, duration } = val;
if (currentTime && duration && currentTime < duration) {
this.hintOperate();
}
}
}
};
</script>
<style lang="less">
#common-video {
position: relative;
.video-box {
box-sizing: border-box;
border: 0;
display: block;
width: 100%;
height: 100%;
outline: none !important;
}
.isShow {
//進(jìn)度條
video::-webkit-media-controls-timeline {
display: none;
}
}
video::-webkit-media-controls-play-button {
visibility: hidden;
}
.operate-btn {
display: block;
width: 60px;
height: 60px;
position: absolute;
top: calc(50% - 30px);
left: calc(50% - 30px);
}
.operate-btn:hover {
opacity: 0.8;
}
.fade-out {
opacity: 0;
}
}
</style>注:
1.使用 isShow 屬性配合 css 樣式動(dòng)態(tài)展示視頻滾動(dòng)條
2.使用 video 標(biāo)簽的 οncοntextmenu=“return false” 屬性來(lái)實(shí)現(xiàn)禁止下載
3.使用 video 標(biāo)簽的 @timeupdate=“timeupdate” 方法來(lái)時(shí)間視頻播放進(jìn)度監(jiān)聽
4.使用 vue 的 ref 屬性為 video 標(biāo)簽綁定監(jiān)聽事件,來(lái)實(shí)現(xiàn)其他功能,如時(shí)長(zhǎng)統(tǒng)計(jì)、延遲提示、動(dòng)態(tài)顯示圖標(biāo)播放按鈕等功能。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue項(xiàng)目中圖片懶加載時(shí)出現(xiàn)的問(wèn)題及解決
這篇文章主要介紹了vue項(xiàng)目中圖片懶加載時(shí)出現(xiàn)的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
vue通過(guò)指令(directives)實(shí)現(xiàn)點(diǎn)擊空白處收起下拉框
這篇文章主要介紹了vue通過(guò)指令(directives)實(shí)現(xiàn)點(diǎn)擊空白處收起下拉框,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
Vue?使用postMessage?實(shí)現(xiàn)父子跨域通信
這篇文章主要介紹了Vue應(yīng)用?postMessage?實(shí)現(xiàn)父子跨域通信,通過(guò)示例介紹了postMessage的使用,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
Vue項(xiàng)目引進(jìn)ElementUI組件的方法
這篇文章主要介紹了Vue項(xiàng)目引進(jìn)ElementUI組件的方法,本文分步驟給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2018-11-11
vue組件表單數(shù)據(jù)回顯驗(yàn)證及提交的實(shí)例代碼
今天小編就為大家分享一篇vue組件表單數(shù)據(jù)回顯驗(yàn)證及提交的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
一文帶你了解vite對(duì)瀏覽器的請(qǐng)求做了什么
Vite是一種新型前端構(gòu)建工具,能夠顯著提升前端開發(fā)體驗(yàn),下面這篇文章主要給大家介紹了關(guān)于vite對(duì)瀏覽器的請(qǐng)求做了什么的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-12-12

