VideoPlay.vue
2.04 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<template>
<div class="videoPlay">
<video
style="object-fit: cover"
:poster="poster"
crossOrigin="anonymous"
ref="videoRef"
class="video-js vjs-default-skin vjs-big-play-centered"
controls
>
<source :src="path" />
</video>
</div>
</template>
<script lang="ts" setup>
import { nextTick, onBeforeUnmount, onMounted, ref, watch, toRefs } from 'vue'
import videojs, { VideoJsPlayer } from 'video.js'
import type { VideoJsPlayerOptions } from 'video.js'
import 'video.js/dist/video-js.css'
import zh from 'video.js/dist/lang/zh-CN.json'
const props = withDefaults(
defineProps<{
path: string
autoPlay?: boolean
h?: number
poster?: string
}>(),
{ autoPlay: false }
)
const videoRef = ref()
let player: VideoJsPlayer
const initPlay = async () => {
videojs.addLanguage('zh-CN', zh)
await nextTick()
const options: VideoJsPlayerOptions = {
muted: true,
controls: true,
autoplay: props.autoPlay,
src: props?.path,
poster: props?.poster,
language: 'zh-CN',
techOrder: ['html5'],
preload: 'none'
}
player = videojs(videoRef.value, options, () => {
videojs.log('Video is reading')
if (props.autoPlay) {
player.play()
}
player.on('ended', () => {
videojs.log('Play end')
})
player.on('error', () => {
player.errorDisplay.close()
videojs.log('Play parse error')
})
})
}
onMounted(() => {
initPlay()
})
//直接改变路径测试
watch(
() => props.path,
() => {
if (props.path && props.autoPlay) {
player?.pause()
player?.load()
player?.src(props.path)
player?.play()
}
},
{
immediate: true
}
)
onBeforeUnmount(() => {
player?.dispose()
})
</script>
<style>
.vjs-poster {
background-size: cover !important;
}
</style>
<style lang="scss" scoped>
.videoPlay {
flex: 1;
height: v-bind('`${h}px`');
.video-js {
height: 100%;
width: 100%;
}
}
</style>