vue3使用video.js播放m3u8格式視頻的操作指南
有時候我們需要播放 m3u8 格式的視頻,或者實現(xiàn)視頻播放器更多定制化需求,HTML 的 video 元素無法實現(xiàn)這些需求,這時候可以考慮使用 Video.js。
video.js是為HTML5世界從零開始構(gòu)建的網(wǎng)絡視頻播放器。它支持HTML5視頻和現(xiàn)代流媒體格式,以及YouTube和Vimeo。
videojs官網(wǎng):videojs.com/guides/
videojs 中文文檔:https://gitcode.gitcode.host/docs-cn/video.js-docs-cn/index.html
實現(xiàn)一個Videojs播放器組件

視頻封面圖片來自unsplash
安裝依賴
npm i video.js
M3U8 是一種基于 HTTP Live Streaming (HLS) 技術的媒體播放列表格式,所以我們還需要安裝依賴。
npm i videojs-contrib-hls
播放器組件代碼
<template>
<div v-bind="$attrs" class="videoPlayer">
<video
class="video-js"
:id="id"
style="width: 100%; height: 100%"
:poster="poster"
>
<source v-if="src" :src="src" />
</video>
</div>
</template>
<script setup lang="ts">
import { onMounted, onBeforeUnmount, watch, ref } from 'vue'
import 'video.js/dist/video-js.css'
import videojs from 'video.js'
const overrideNative = ref(false)
const props = defineProps({
id: { type: String, default: 'vd' },
src: { type: String, default: '' },
poster: { type: String, default: '' }
})
const emit = defineEmits([
'videoCanplaythrough',
'videoPlay',
'videoPlaying',
'videoPause'
])
// VideoJs更多選項配置可以參考中文文檔:
// https://gitcode.gitcode.host/docs-cn/video.js-docs-cn/docs/guides/options.html
function options() {
return {
html5: {
hls: {
overrideNative: overrideNative
},
nativeVideoTracks: !overrideNative,
nativeAudioTracks: !overrideNative,
nativeTextTracks: !overrideNative
},
autoplay: true, // true,瀏覽器準備好時開始播放。
muted: false, // 默認情況下將會消除音頻。
loop: false, // 導致視頻一結(jié)束就重新開始。
controls: true,
preload: 'auto', // auto瀏覽器選擇最佳行為,立即開始加載視頻(如果瀏覽器支持)
fluid: true, // 當true時,將按比例縮放以適應其容器。
type: 'application/x-mpegURl',
notSupportedMessage: '此視頻暫無法播放,請稍后再試', // 無法播放媒體源時顯示的默認信息。
textTrackDisplay: false
}
}
let player: any
onMounted(() => {
try {
player = videojs(props.id, options(), () => {
videojs.log('播放器已經(jīng)準備好了!')
player.pause()
player.on('canplaythrough', function (event: any) {
emit('videoCanplaythrough', event.target.player.cache_?.duration)
})
player.on('play', function () {
videojs.log('視頻準備播放')
emit('videoPlay')
})
player.on('playing', function () {
videojs.log('視頻已開始播放')
emit('videoPlaying')
})
player.on('pause', function (event: any) {
emit('videoPause', event.target.player.cache_?.currentTime)
})
})
} catch (error) {
console.log('catch--->', error)
}
})
onBeforeUnmount(() => {
if (player) {
player.dispose()
}
})
</script>
<style scoped>
.videoPlayer {
width: 100%;
max-width: 640px;
height: 360px;
position: relative;
overflow: hidden;
}
.video-js {
padding-top: 0 !important;
}
</style>
組件使用
<template>
<div>
<VideoPlayer
:src="src"
id="videoPlayer"
:poster="require('~/assets/images/photo-1622208489373-1fe93e2c6720.avif')"
/>
</div>
</template>
<script setup>
import { ref } from 'vue'
const src = ref(
'https://xxx.m3u8'
)
</script>
設置語言為中文
基礎的播放器已經(jīng)寫好了,但是現(xiàn)在播放器自帶的語言還是英文,我們需要設置為中文。 VideoJS自帶了很多語言包,按需設置即可。

import video_zhCN from 'video.js/dist/lang/zh-CN.json'
videojs.addLanguage('zh-CN', video_zhCN)
// ...
function options() {
return {
// ...
language: 'zh-CN'
}
}
OK,VideoJs 播放器的文字已經(jīng)變成中文了。

如何同時使用多個VideoJs播放器組件
如果我們直接復制一樣的組件代碼,發(fā)現(xiàn)只有第一個可以正常播放,第二個播放器不能使用

這個問題也很好解決,上面的組件props提供了id屬性,我們只需給兩個組件設置不同的id即可
<VideoPlayer
:src="src"
id="vd1"
:poster="require('~/assets/images/photo-1622208489373-1fe93e2c6720.avif')"
/>
你可能還會遇到切換視頻沒有變化的的問題,通過為每次播放的視頻設置獨一無二的id即可,類似:id=uuid()
指定播放時間點
通過設置currentTime,可以讓視頻從某個時間點開始播放
watch(
() => props.currentTime,
() => {
player.currentTime(Number(props.currentTime))
}
)
onMounted(() => {
try {
player = videojs(props.id, options(), () => {
// ...
player.on('timeupdate', function (event: any) {
emit('videoTimeupdate', event.target.player.cache_?.currentTime)
})
// ...
})
}
})
以上就是vue3使用video.js播放m3u8格式視頻的操作指南的詳細內(nèi)容,更多關于vue3 video.js播放m3u8視頻的資料請關注腳本之家其它相關文章!
相關文章
Vue項目中Element UI組件未注冊的問題原因及解決方法
在 Vue 項目中使用 Element UI 組件庫時,開發(fā)者可能會遇到一些常見問題,例如組件未正確注冊導致的警告或錯誤,本文將詳細探討這些問題的原因、解決方法,以及如何在需要時撤銷相關操作,需要的朋友可以參考下2025-01-01
Vue的雙向數(shù)據(jù)綁定實現(xiàn)原理解析
這篇文章主要介紹了Vue的雙向數(shù)據(jù)綁定實現(xiàn)原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-02-02
基于electron+vue3+ts搭建桌面端應用并且可以熱更新
這篇文章主要為大家詳細介紹了如何基于electron+vue3+ts搭建桌面端應用并且可以熱更新,文中的示例代碼講解詳細,感興趣的小伙伴可以參考下2023-10-10
vue3.0?router路由跳轉(zhuǎn)傳參問題(router.push)
這篇文章主要介紹了vue3.0?router路由跳轉(zhuǎn)傳參問題(router.push),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02

