這篇文章主要為大家展示了“Vue如何使用video標簽實現視頻播放”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“Vue如何使用video標簽實現視頻播放”這篇文章吧。
具體內容如下
項目需求:動態顯示視頻滾動條、禁止視頻下載、播放時每5s更新當前時長、每10分鐘暫停視頻顯示提示。
之前做視頻播放時基本都是使用 vue-video-player 組件,其原因為 封裝功能齊全、播放源兼容性好等。
通過這次項目需求,也深入的學習了 video 標簽的屬性及方法。具體在這里跟大家分享一下。
具體使用方法如下
<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, // 視頻播放狀態
// 學時
studyTime: {
currentTime: 0, // 當前已學時長
duration: 0 // 總時長
},
timer: {}, // 定時器
pauseTimer: {} // 暫停定時器
};
},
/**
* @param poster 展示圖
* @param src 視頻資源
* @param controls 是否顯示控件
* @param control 控件控制
* @param videoData 視頻基礎數據
*/
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() {
// 監聽視頻播放
this.$refs.myVideo.addEventListener("play", () => {
console.log("video is playing");
this.openTimer();
});
// 監聽視頻暫停
this.$refs.myVideo.addEventListener("pause", () => {
console.log("video is stop");
this.closeTimer();
});
},
methods: {
// 開啟定時器
openTimer() {
this.timer = setInterval(() => {
this.$emit("videoStudyTime", this.studyTime);
}, 5000);
},
// 關閉定時器
closeTimer() {
clearInterval(this.timer);
this.$emit("videoStudyTime", this.studyTime);
},
// 開啟暫停定時器
openPauseTimer() {
this.pauseTimer = setInterval(() => {
this.hintOperate();
}, 600000);
},
// 關閉暫停定時器
closePauseTimer() {
clearInterval(this.pauseTimer);
},
// 提示操作
hintOperate() {
this.operateVideo();
this.$alert("請點擊確認繼續學習", "提示", {
confirmButtonText: "確定",
confirmButtonClass: "hint-btn",
showClose: false,
callback: action => {
this.$refs.myVideo.currentTime = this.videoData.currentTime;
this.operateVideo();
this.openPauseTimer();
}
});
},
// 獲取當前播放位置
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: "暫無視頻資源,請查看其他視頻!" });
return false;
}
if (this.$refs.myVideo.paused) {
this.$refs.myVideo.play();
this.videoState = true;
} else {
this.$refs.myVideo.pause();
this.videoState = false;
}
}
},
watch: {
// 監聽操作
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 {
//進度條
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 樣式動態展示視頻滾動條
2.使用 video 標簽的 οncοntextmenu=“return false” 屬性來實現禁止下載
3.使用 video 標簽的 @timeupdate=“timeupdate” 方法來時間視頻播放進度監聽
4.使用 vue 的 ref 屬性為 video 標簽綁定監聽事件,來實現其他功能,如時長統計、延遲提示、動態顯示圖標播放按鈕等功能。
以上是“Vue如何使用video標簽實現視頻播放”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。