vue3使用video.js播放m3u8格式視頻的操作指南
有時候我們需要播放 m3u8 格式的視頻,或者實現(xiàn)視頻播放器更多定制化需求,HTML 的 video 元素?zé)o法實現(xiàn)這些需求,這時候可以考慮使用 Video.js。
video.js是為HTML5世界從零開始構(gòu)建的網(wǎng)絡(luò)視頻播放器。它支持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) 技術(shù)的媒體播放列表格式,所以我們還需要安裝依賴。
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,瀏覽器準(zhǔn)備好時開始播放。 muted: false, // 默認(rèn)情況下將會消除音頻。 loop: false, // 導(dǎo)致視頻一結(jié)束就重新開始。 controls: true, preload: 'auto', // auto瀏覽器選擇最佳行為,立即開始加載視頻(如果瀏覽器支持) fluid: true, // 當(dāng)true時,將按比例縮放以適應(yīng)其容器。 type: 'application/x-mpegURl', notSupportedMessage: '此視頻暫無法播放,請稍后再試', // 無法播放媒體源時顯示的默認(rèn)信息。 textTrackDisplay: false } } let player: any onMounted(() => { try { player = videojs(props.id, options(), () => { videojs.log('播放器已經(jīng)準(zhǔn)備好了!') player.pause() player.on('canplaythrough', function (event: any) { emit('videoCanplaythrough', event.target.player.cache_?.duration) }) player.on('play', function () { videojs.log('視頻準(zhǔn)備播放') 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>
設(shè)置語言為中文
基礎(chǔ)的播放器已經(jīng)寫好了,但是現(xiàn)在播放器自帶的語言還是英文,我們需要設(shè)置為中文。 VideoJS自帶了很多語言包,按需設(shè)置即可。
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ù)制一樣的組件代碼,發(fā)現(xiàn)只有第一個可以正常播放,第二個播放器不能使用
這個問題也很好解決,上面的組件props提供了id屬性,我們只需給兩個組件設(shè)置不同的id即可
<VideoPlayer :src="src" id="vd1" :poster="require('~/assets/images/photo-1622208489373-1fe93e2c6720.avif')" />
你可能還會遇到切換視頻沒有變化的的問題,通過為每次播放的視頻設(shè)置獨(dú)一無二的id即可,類似:id=uuid()
指定播放時間點
通過設(shè)置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格式視頻的操作指南的詳細(xì)內(nèi)容,更多關(guān)于vue3 video.js播放m3u8視頻的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Element-ui設(shè)置el-table表頭全選框隱藏或禁用
這篇文章主要給大家介紹了關(guān)于Element-ui設(shè)置el-table表頭全選框隱藏或禁用的相關(guān)資料,文中手把手教你實現(xiàn)el-table實現(xiàn)跨表格禁用選項,需要的朋友可以參考下2023-07-07window.onresize在vue中只能使用一次,自適應(yīng)resize報錯問題
這篇文章主要介紹了window.onresize在vue中只能使用一次,自適應(yīng)resize報錯問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10vue通過指令(directives)實現(xiàn)點擊空白處收起下拉框
這篇文章主要介紹了vue通過指令(directives)實現(xiàn)點擊空白處收起下拉框,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12vue3+ElementPlus+VueCropper實現(xiàn)上傳圖片功能
文章介紹了如何在Vue3、ElementPlus和VueCropper組件中實現(xiàn)圖片上傳和裁剪功能,包括放大、縮小等操作,感興趣的朋友跟隨小編一起看看吧2025-01-01vue3.0運(yùn)行npm run dev報錯Cannot find module&
本文主要介紹了vue3.0運(yùn)行npm run dev報錯Cannot find module node:url,因為使用的node版本是14.15.1低于15.0.0導(dǎo)致,具有一定的參考價值,感興趣的可以了解一下2023-10-10