1、背景
項(xiàng)目中有涉及視頻播放的需求,并且UI設(shè)計(jì)了樣式,與原生的視頻video組件有差異,所以使用了vue-video-player插件,并對vue-video-player進(jìn)行樣式改造,自定義播放暫停按鈕、全屏按鈕、時(shí)間進(jìn)度條樣式等,自動播放設(shè)置、設(shè)置一開始全屏播放視頻、監(jiān)聽全屏事件等。
2、效果圖
這是樣式處理后的效果:
這是未處理樣式的效果:
3、代碼實(shí)現(xiàn)
3.1 安裝插件
我安裝的是指定版本
npm install vue-video-player@5.0.1 --save
,因?yàn)槲抑苯影惭b最新版本npm install vue-video-player --save
項(xiàng)目就會報(bào)錯(cuò),如果你們安裝最新版本沒報(bào)錯(cuò)也可以安裝最新版。
安裝vue-video-player插件后不需要再安裝videojs插件,因?yàn)関ue-video-player已經(jīng)包含有videojs在里面了
3.2 引入、注冊插件
在main.js文件中,填入以下代碼
import VideoPlayer from 'vue-video-player/src'
import 'vue-video-player/src/custom-theme.css'
import 'video.js/dist/video-js.css'
Vue.use(VideoPlayer)
3.3 創(chuàng)建組件
在components文件夾下(隨意位置,導(dǎo)入的時(shí)候更改文件路徑就行),新建testVideo.vue文件,填入以下代碼
<template>
<div class="video">
<!-- 使用組件 -->
<video-player class="video-player vjs-custom-skin"
ref="videoPlayer"
:playsinline="true"
:options="playerOptions"
></video-player>
</div>
</template>
<script>
// 以下三行一定要引入
import { videoPlayer } from 'vue-video-player'
import 'video.js/dist/video-js.css'
import 'vue-video-player/src/custom-theme.css'
// import 'video.js/dist/lang/zh-CN'
export default {
// name: 'videoplayer',
components: { // 必需引入
videoPlayer
},
props: [ // 接收父組件的數(shù)據(jù)
'mp4Pic',
'mp4Url'
],
data () {
return {
fileAreaHeight: 675,
fileType: 'mp4', // 資源的類型
}
},
computed: { // 使用計(jì)算屬性
playerOptions () {
const playerOptionsObj = {
playbackRates: [0.7, 1.0, 1.5, 2.0], //視頻播放速度
autoplay: false, // 如果true,瀏覽器準(zhǔn)備好時(shí)開始回放。
muted: false, // 默認(rèn)情況下將會消除任何音頻。
loop: false, // 導(dǎo)致視頻一結(jié)束就重新開始。
// preload: 'auto', // 建議瀏覽器在<video>加載元素后是否應(yīng)該開始下載視頻數(shù)據(jù)。auto瀏覽器選擇最佳行為,立即開始加載視頻(如果瀏覽器支持)。
language: 'zh-CN',
// aspectRatio: '16:9', // 將播放器置于流暢模式,并在計(jì)算播放器的動態(tài)大小時(shí)使用該值。值應(yīng)該代表一個(gè)比例 - 用冒號分隔的兩個(gè)數(shù)字(例如"16:9"或"4:3")。
fluid: false, // 當(dāng)true時(shí),Video.js player將擁有流體大小。換句話說,它將按比例縮放以適應(yīng)其容器。
sources: [{
type: 'video/' + this.fileType, // 資源格式寫法:'video/mp4',否則控制臺會出現(xiàn)notSupportedMessage設(shè)置的錯(cuò)誤。
src: this.mp4Url // 視頻url地址
}],
poster: this.mp4Pic, // 視頻封面地址
// width: document.documentElement.clientWidth,
width: 1200,
height: this.fileAreaHeight, // 設(shè)置高度,fluid需要設(shè)置成flase
notSupportedMessage: '此視頻暫無法播放...', // 允許覆蓋Video.js無法播放媒體源時(shí)顯示的默認(rèn)信息。
controlBar: {
timeDivider: true, // 當(dāng)前時(shí)間和持續(xù)時(shí)間的分隔符
durationDisplay: true, // 顯示持續(xù)時(shí)間
remainingTimeDisplay: false, // 是否顯示剩余時(shí)間功能
fullscreenToggle: true //全屏按鈕
}
}
return playerOptionsObj
}
},
watch: {
}
}
</script>
<style>
.video /*可不設(shè)置*/
{
margin: 48px 0;
}
.vjs-poster {
background-color: #aaaaaa00;
}
/*播放按鈕設(shè)置成寬高一致,圓形,居中*/
.vjs-custom-skin > .video-js .vjs-big-play-button {
outline: none;
border: none;
width: 66px;
height: 66px !important;
background-color: rgba(0,0,0,0) !important;
}
.video-js .vjs-big-play-button .vjs-icon-placeholder:before {
content: '';
width: 66px;
height: 66px;
background: url('../assets/icon_stop@2x.png') no-repeat;
background-size: 100% 100%;
}
/*control-bar布局時(shí)flex,通過order調(diào)整剩余時(shí)間的位置到進(jìn)度條右邊*/
.vjs-custom-skin > .video-js .vjs-control-bar .vjs-remaining-time{
order:3 !important;
}
/* 進(jìn)度條下面的播放按鈕 */
.vjs-custom-skin > .video-js .vjs-control-bar .vjs-play-control {
margin: 0;
line-height: 20px;
height: 94px;
padding: 50px 0 24px 0;
}
.vjs-custom-skin > .video-js .vjs-control-bar .vjs-play-control .vjs-icon-placeholder:before {
position: absolute;
font-size: 20px;
top: 44px;
left: 24px;
width: 20px;
height: 20px;
}
/** 時(shí)間組件 */
.vjs-custom-skin > .video-js .vjs-control-bar .vjs-time-control {
margin: 0;
line-height: 20px;
height: 94px;
padding: 50px 0 24px 0;
min-width: auto;
}
/* 時(shí)間-左 */
.video-js .vjs-current-time, .vjs-no-flex .vjs-current-time {
min-width: 32px;
font-size: 12px;
font-family: PingFangSC-Regular, PingFang SC;
font-weight: 400;
color: #FFFFFF;
line-height: 20px;
margin: 0 0 0 24px !important;
}
/* 下面控件的時(shí)間分割線 */
.vjs-custom-skin > .video-js .vjs-control-bar .vjs-time-divider {
min-width: 6px;
margin: 0 8px !important;
font-size: 12px;
font-family: PingFangSC-Regular, PingFang SC;
font-weight: 400;
color: rgba(255,255,255,0.2);
line-height: 20px;
}
/* 時(shí)間-右 */
.video-js .vjs-duration, .vjs-no-flex .vjs-duration {
font-size: 12px;
font-family: PingFangSC-Regular, PingFang SC;
font-weight: 400;
color: #FFFFFF;
line-height: 20px;
}
.video-js .vjs-control-bar {
height: 94px;
background: linear-gradient(180deg, rgba(0,0,0,0) 0%, rgba(0,0,0,0.7) 100%)
}
/*進(jìn)度條單獨(dú)放置一行*/
.vjs-custom-skin > .video-js .vjs-progress-control.vjs-control{
position: absolute;
left: 0;
right: 0;
bottom: 68px;
width: 100%;
height: 2px;
padding: 0 24px;
}
/* 進(jìn)度條背景軌道 */
.video-js .vjs-slider{
border-radius: 1em;
background-color: rgba(255,255,255,0.2);
}
/* 加載進(jìn)度條背景色 */
.video-js .vjs-load-progress {
background: rgba(255,255,255,0.1);
}
/* 進(jìn)度條進(jìn)度 */
.vjs-custom-skin > .video-js .vjs-play-progress, .vjs-custom-skin > .video-js .vjs-volume-level{
border-radius: 1px;
background: #FFFFFF;
}
/*鼠標(biāo)進(jìn)入播放器后,播放按鈕顏色會變*/
.video-js:hover .vjs-big-play-button, .vjs-custom-skin>.video-js .vjs-big-play-button:active, .vjs-custom-skin>.video-js .vjs-big-play-button:focus{
background-color: rgba(0,0,0,0) !important;
}
/*control bar*/
.video-js .vjs-control-bar{
background-color: rgba(0,0,0,0.2) !important;
}
/*點(diǎn)擊按鈕時(shí)不顯示藍(lán)色邊框*/
.video-js .vjs-control-bar button{
outline: none;
}
.vjs-volume-panel .vjs-control .vjs-volume-panel-horizontal {
width: 0;
display: none;
}
/** 隱藏倍速 */
.vjs-custom-skin > .video-js .vjs-control-bar .vjs-playback-rate {
display: none;
}
/** 音量按鈕 */
.video-js .vjs-volume-panel {
/* display: none; */
position: absolute;
right: 48px;
bottom: 24px;
width: 20px;
height: 20px;
}
.vjs-icon-volume-high:before, .video-js .vjs-mute-control .vjs-icon-placeholder:before {
font-size: 20px;
width: 20px;
height: 20px;
line-height: 20px;
color: rgba(255,255,255,0.9);
}
.video-js .vjs-volume-bar {
margin: 8px 16px 8px 0;
}
.video-js .vjs-volume-level {
left: -21px;
}
/* 全屏組件 */
.vjs-custom-skin > .video-js .vjs-control-bar .vjs-fullscreen-control {
position: absolute;
right: 24px;
bottom: 24px;
width: 20px;
height: 20px;
}
.video-js .vjs-big-play-button .vjs-icon-placeholder:before, .vjs-button > .vjs-icon-placeholder:before {
text-align: right;
}
/* 全屏按鈕圖標(biāo) */
.vjs-icon-fullscreen-enter:before, .video-js .vjs-fullscreen-control .vjs-icon-placeholder:before {
content: '';
width: 20px;
height: 20px;
background: url('../assets/icon_full@2x.png') no-repeat;
background-size: 100% 100%;
}
/* 全屏播放后隱藏自定義全屏圖標(biāo) */
.vjs-icon-fullscreen-exit:before, .video-js.vjs-fullscreen .vjs-fullscreen-control .vjs-icon-placeholder:before {
background: url('');
line-height: 20px;
margin-right: 10px;
}
</style>
樣式文件里的圖片及路徑,大家根據(jù)自己的實(shí)際情況進(jìn)行調(diào)整
3.4 完成效果圖!其實(shí)最重要的就是樣式調(diào)整這里。。。
4、設(shè)置自動播放
1、將
muted
屬性設(shè)置為true
2、調(diào)用視頻播放事件
// 設(shè)置自動播放事件中,將下面的代碼放進(jìn)去
this.$refs.videoPlayer.player.load()
this.$refs.videoPlayer.player.play()
5、設(shè)置一開始全屏播放視頻
在全屏播放事件中,將下面的代碼放進(jìn)去文章來源:http://www.zghlxwxcb.cn/news/detail-477809.html
this.$refs.videoPlayer.player.requestFullscreen()
6、監(jiān)聽視頻的全屏事件
視頻的全屏事件,在網(wǎng)上查了好久的資料,都沒有找到解決辦法,查chatGPT也是報(bào)錯(cuò),最后結(jié)合網(wǎng)上的例子,一個(gè)一個(gè)試出來,暫時(shí)沒發(fā)現(xiàn)有問題,可以獲取到視頻是否全屏狀態(tài)。文章來源地址http://www.zghlxwxcb.cn/news/detail-477809.html
<video-player
class="video-player vjs-custom-skin"
ref="videoPlayer"
:playsinline="true"
:options="playerOptions"
@loadeddata="onLoadedData"
></video-player>
methods: {
/** 監(jiān)聽視頻 */
onLoadedData(player) {
player.on('fullscreenchange', () => {
// player.isFullscreen_就是全屏的狀態(tài),true為全屏,false為不全屏
// 在這里處理你的需求
})
},
}
到了這里,關(guān)于vue 視頻播放插件vue-video-player自定義樣式、自動播放設(shè)置、設(shè)置一開始全屏播放視頻、監(jiān)聽全屏事件的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!