詳解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)文章
使用WebStorm用Debug模式調(diào)試Vue等前端項目的步驟
WebStorm提供了更簡單的前端調(diào)試方法,記錄一下WebStorm調(diào)試步驟啟動前端項目,這篇文章主要介紹了使用WebStorm用Debug模式調(diào)試Vue等前端項目的步驟,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2024-11-11
vue使用vue-video-player無法播放本地視頻的問題及解決
這篇文章主要介紹了vue使用vue-video-player無法播放本地視頻的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
vue使用v-if v-show頁面閃爍,div閃現(xiàn)的解決方法
在頁面層次結(jié)構(gòu),數(shù)據(jù)較多的時候,用v-if或者v-show就會出現(xiàn)div閃現(xiàn),或者部分閃爍的結(jié)果。怎么處理這樣的問題呢,下面小編給大家?guī)砹藇ue使用v-if v-show頁面閃爍,div閃現(xiàn)的解決方法,一起看看吧2018-10-10
vue導(dǎo)入excel文件,vue導(dǎo)入多個sheets的方式
這篇文章主要介紹了vue導(dǎo)入excel文件,vue導(dǎo)入多個sheets的方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08

