hookehuyr

refactor(VideoPlayer): 使用VideoPlayer组件替换原生video标签

重构VideoPlayer组件,使用@videojs-player/vue库中的VideoPlayer组件替换原生video标签,以提高代码的可维护性和扩展性。同时,将播放器配置集中到videoOptions计算属性中,支持通过props传递自定义配置。
1 -<!--
2 - * @Date: 2025-03-24 15:13:35
3 - * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2025-03-24 15:13:37
5 - * @FilePath: /mlaj/src/components/ui/VideoPlayer.vue
6 - * @Description: 文件描述
7 --->
8 <template> 1 <template>
9 <div class="video-player-container"> 2 <div class="video-player-container">
10 - <video 3 + <VideoPlayer
11 ref="videoRef" 4 ref="videoRef"
12 - class="video-js vjs-default-skin" 5 + :options="videoOptions"
6 + @ready="onPlayerReady"
13 controls 7 controls
14 - preload="auto" 8 + />
15 - width="100%"
16 - height="100%"
17 - >
18 - <source :src="videoUrl" type="video/mp4" />
19 - </video>
20 </div> 9 </div>
21 </template> 10 </template>
22 11
23 <script setup> 12 <script setup>
24 -import { ref, onMounted, onBeforeUnmount, defineProps, defineEmits } from 'vue' 13 +import { ref, computed, onMounted, onBeforeUnmount, defineProps, defineEmits } from 'vue'
14 +import { VideoPlayer } from '@videojs-player/vue'
25 import videojs from 'video.js' 15 import videojs from 'video.js'
26 import 'video.js/dist/video-js.css' 16 import 'video.js/dist/video-js.css'
27 17
28 const props = defineProps({ 18 const props = defineProps({
19 + options: {
20 + type: Object,
21 + required: false,
22 + default: () => ({})
23 + },
29 videoUrl: { 24 videoUrl: {
30 type: String, 25 type: String,
31 required: true 26 required: true
...@@ -36,22 +31,24 @@ const emit = defineEmits(['onPlay', 'onPause']) ...@@ -36,22 +31,24 @@ const emit = defineEmits(['onPlay', 'onPause'])
36 const videoRef = ref(null) 31 const videoRef = ref(null)
37 let player = null 32 let player = null
38 33
39 -onMounted(() => { 34 +const videoOptions = computed(() => ({
40 - player = videojs(videoRef.value, { 35 + fluid: true,
41 - fluid: true, 36 + controls: true,
42 - controls: true, 37 + preload: 'auto',
43 - preload: 'auto', 38 + responsive: true,
44 - responsive: true 39 + autoplay: props.options?.autoplay || false,
45 - }) 40 + sources: [{
41 + src: props.videoUrl,
42 + type: 'video/mp4'
43 + }],
44 + onPlay: () => emit('onPlay'),
45 + onPause: () => emit('onPause'),
46 + ...props.options
47 +}))
46 48
47 - player.on('play', () => { 49 +const onPlayerReady = (instance) => {
48 - emit('onPlay') 50 + player = instance
49 - }) 51 +}
50 -
51 - player.on('pause', () => {
52 - emit('onPause')
53 - })
54 -})
55 52
56 onBeforeUnmount(() => { 53 onBeforeUnmount(() => {
57 if (player) { 54 if (player) {
......