詳解vue3中如何使用shaka-player
正文
Shaka Player 是谷歌公司對外開源的一款 JavaScript 類庫,詳細(xì)請看谷歌官方API文檔。
開始使用
我們可以使用 npm 下載
npm i shaka-player
做成組件shakaPlayer
<script setup>
import { ref, watch, onMounted, onBeforeUnmount } from "vue";
import shaka from "shaka-player/dist/shaka-player.ui.js";
import "../../node_modules/shaka-player/dist/controls.css"; // 注意路徑
const props = defineProps({
src: { type: String, required: true },
poster: { type: String, default: "" },
autoplay: { type: Boolean, default: false }
});
onMounted(() => {
initApp();
});
onBeforeUnmount(() => {
player && player.destroy();
});
const initApp = () => {
if (shaka.Player.isBrowserSupported()) {
initPlayer();
} else {
console.error("Browser not supported!");
}
};
const videoPlayer = ref();
const videoContainer = ref();
let player = null;
const initPlayer = () => {
player = new shaka.Player(videoPlayer.value);
const ui = new shaka.ui.Overlay(
player,
videoContainer.value,
videoPlayer.value
);
ui.configure({
// 自定義控件
controlPanelElements: [
"time_and_duration", // 進(jìn)度
"spacer",
"mute", // 靜音
"volume", // 音量
"fullscreen", // 全屏
"overflow_menu"
],
overflowMenuButtons: [
"picture_in_picture", // 畫中畫
"playback_rate" // 倍速
],
playbackRates: [0.5, 1, 1.5, 2], // 倍速選項
// 視頻進(jìn)入全屏?xí)r設(shè)備是否應(yīng)旋轉(zhuǎn)到橫向模式
forceLandscapeOnFullscreen: false
});
loadPlayer();
};
const loadPlayer = async () => {
try {
await player.load(props.src);
} catch (e) {
onError(e);
}
};
const onError = (error) => {
console.error("Error code", error.code, "object", error);
};
const play = () => videoPlayer.value && videoPlayer.value.play();
const pause = () => videoPlayer.value && videoPlayer.value.pause();
watch(
() => props.src,
() => initPlayer()
);
defineExpose({ play, pause });
</script>
<template>
<div ref="videoContainer" class="max-w-full">
<video
ref="videoPlayer"
class="full"
:poster="poster"
:autoplay="autoplay"
muted
></video>
</div>
</template>
<style scoped>
.max-w-full {
max-width: 100%;
}
.full {
width: 100%;
height: 100%;
}
</style>
使用方式
<shaka-player class="video" :src="src" :poster="poster" autoplay ></shaka-player>
.video {
width: 100%;
height: 200px;
}

注意事項
默認(rèn)視頻控件是顯示所有的,允許我們自定義。
我們可以直接使用 video 原生方法、事件和屬性。
寬高可以用class樣式設(shè)置。
Shaka Player不支持Vue響應(yīng)對象,player 不能用 ref 來聲明。
移動端視頻默認(rèn)靜音時,autoplay 才會生效。
以上就是詳解vue3中如何使用shaka-player的詳細(xì)內(nèi)容,更多關(guān)于vue3使用shaka-player的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
解決Vue-cli npm run build生產(chǎn)環(huán)境打包,本地不能打開的問題
今天小編就為大家分享一篇解決Vue-cli npm run build生產(chǎn)環(huán)境打包,本地不能打開的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
Vue封裝一個Tabbar組件?帶組件路由跳轉(zhuǎn)方式
這篇文章主要介紹了Vue封裝一個Tabbar組件?帶組件路由跳轉(zhuǎn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04
vue3 template轉(zhuǎn)為render函數(shù)過程詳解
在 Vue 中,template 模板是我們編寫組件的主要方式之一,而 Vue 內(nèi)部會將這些模板轉(zhuǎn)換為 render 函數(shù),render 函數(shù)是用于創(chuàng)建虛擬 DOM 的函數(shù),通過它,Vue 能夠高效地追蹤 DOM 的變化并進(jìn)行更新,下面我會通俗易懂地詳細(xì)解釋 Vue 如何將 template 轉(zhuǎn)換為 render 函數(shù)2024-10-10
Vue實現(xiàn)戶籍管理系統(tǒng)戶籍信息的添加與刪除方式
這篇文章主要介紹了Vue實現(xiàn)戶籍管理系統(tǒng)戶籍信息的添加與刪除方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09

