详解vue3中如何使用shaka-player
更新时间:2022年09月21日 09:09:10 作者:简单卟容易
这篇文章主要为大家介绍了vue3中如何使用shaka-player示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
正文
Shaka Player 是谷歌公司对外开源的一款 JavaScript 类库,详细请看谷歌官方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", // 进度
"spacer",
"mute", // 静音
"volume", // 音量
"fullscreen", // 全屏
"overflow_menu"
],
overflowMenuButtons: [
"picture_in_picture", // 画中画
"playback_rate" // 倍速
],
playbackRates: [0.5, 1, 1.5, 2], // 倍速选项
// 视频进入全屏时设备是否应旋转到横向模式
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;
}

注意事项
默认视频控件是显示所有的,允许我们自定义。
我们可以直接使用 video 原生方法、事件和属性。
宽高可以用class样式设置。
Shaka Player不支持Vue响应对象,player 不能用 ref 来声明。
移动端视频默认静音时,autoplay 才会生效。
以上就是详解vue3中如何使用shaka-player的详细内容,更多关于vue3使用shaka-player的资料请关注脚本之家其它相关文章!
相关文章
使用WebStorm用Debug模式调试Vue等前端项目的步骤
WebStorm提供了更简单的前端调试方法,记录一下WebStorm调试步骤启动前端项目,这篇文章主要介绍了使用WebStorm用Debug模式调试Vue等前端项目的步骤,文中通过图文介绍的非常详细,需要的朋友可以参考下2024-11-11
vue使用vue-video-player无法播放本地视频的问题及解决
这篇文章主要介绍了vue使用vue-video-player无法播放本地视频的问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-08-08
vue使用v-if v-show页面闪烁,div闪现的解决方法
在页面层次结构,数据较多的时候,用v-if或者v-show就会出现div闪现,或者部分闪烁的结果。怎么处理这样的问题呢,下面小编给大家带来了vue使用v-if v-show页面闪烁,div闪现的解决方法,一起看看吧2018-10-10


最新评论