起因
同时在vue3
中播放多个视频, 还能翻页.
问题
问题1.多个播放器
解决: 按照videojs
官方文档的vue
集成教程熟悉下, 在封装成组件, 一个组件有一个播放器, 循环就支持多个. 通过ref
来自动生成id
.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31<template>
<div>
<a href="#" class="group" v-for="item in items">
<div class="aspect-w-1 aspect-h-1 w-full overflow-hidden">
<video-player :options="item.videoOptions" />
</div>
<h3 class="mt-1 text-lg font-medium text-gray-900">{{ item.name }}</h3>
</a>
</div>
</template>
<script setup>
import { ref } from "vue";
import VideoPlayer from '@/components/video-player.vue'
const items = [
{
name:"湖南卫视",
videoOptions: {
autoplay: true,
muted: true,
controls: true,
sources: [
{
src: 'http://219.151.31.38/liveplay-kk.rtxapp.com/live/program/live/hnwshd/4000000/mnf.m3u8',
type: 'application/x-mpegURL'
}
]
}
}]
</script>
问题2.翻页播放
翻页后videojs
生成的id未变, 视频还是更改前的. vue组件传进去的参数已经发生改变, 但videojs
实例player
的播放地址没有改变. 看官方文档有个player.src()
方法可以改变播放地址. 却出现一直请求.m3u8
文件, 未请求.ts
视频文件.
解决:
1.在组件内通过watch
来监听参数变化, 有变动通过player.src()
来设置新的播放地址.
2.由于vue3
默认使用代理来声明变量, player
的src()
就出现了问题, 可以把player
声明为普通变量, 可以参考video.js/issues/7418
.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70// video-player.vue
<script>
import videojs from 'video.js';
import "video.js/dist/video-js.css";
import zh from "video.js/dist/lang/zh-CN.json";
import { onBeforeUnmount, onMounted, watch, ref } from 'vue';
videojs.addLanguage('zh-CN', zh);
export default {
name: 'LjVideoPlayer',
props: {
options: {
type: Object,
default() {
return {};
}
}
},
setup(props) {
let videoPlayer = ref(null)
let player =null;
const onChange = () => {
if (player) {
player.src(props.options.sources)
}
}
const dispose = () => {
if (player) {
player.dispose()
}
}
onMounted(() => {
player = videojs(videoPlayer.value, props.options, () => {
// player.log('onPlayerReady', this);
});
})
onBeforeUnmount(() => {
if (player) {
player.dispose();
}
})
watch(props, () => {
onChange()
})
return { dispose, videoPlayer,}
},
}
</script>
<template>
<div>
<video ref="videoPlayer" class="video-js vjs-fluid vjs-fill" webkit-playsinline="true" playsinline="true"></video>
</div>
</template>
<style scoped>
video {
height: 100%;
width: 100%;
min-width: 100px;
min-height: 100px;
display: block;
}
</style>
问题3. vue3没有$refs
videojs
初始化需要元素, 通过ref
来指定后, 在取值时需要声明和导出.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23<script>
export default {
setup(){
// 声明
let videoPlayer = ref(null)
let player =null
onMounted(() => {
player = videojs(videoPlayer.value, props.options, () => {
// player.log('onPlayerReady', this);
});
})
// 导出
return {videoPlayer,}
}
}
</script>
<template>
<div>
<video ref="videoPlayer"></video>
</div>
</template>
问题4. 中文语言
官方文档只说了设置语言, 却没说如何引入中文语言包. 没有说明vue
如何引入中文包, 在https://github.com/videojs/video.js/issues/7986
可以知道.1
2// 引入中文
import zh from "video.js/dist/lang/zh-CN.json";
1 | // 初始化的时候设置中文 |
问题5. 重复使用一个弹出框来播放不同视频
一种是弹出生成播放实例, 关闭后销毁. 一种是重新设置视频地址.
下面说说不销毁的, 如果不销毁, 那么需要使用暂停, 会一直请求视频地址(m3u8)来缓存. 可以设置视频地址为空字符串, 来禁止一直请求.1
2
3player.pause()
player.src('')
player.load()