refactor(VideoPlayer): 优化视频播放器组件代码结构
重构视频播放器组件,使用 ref 替代 let 定义 player 和 state,增加 handleMounted 方法处理挂载逻辑,并调整视频播放器配置以支持更多用户操作和控件优化
Showing
1 changed file
with
29 additions
and
7 deletions
| ... | @@ -3,8 +3,10 @@ | ... | @@ -3,8 +3,10 @@ |
| 3 | <VideoPlayer | 3 | <VideoPlayer |
| 4 | ref="videoRef" | 4 | ref="videoRef" |
| 5 | :options="videoOptions" | 5 | :options="videoOptions" |
| 6 | - @ready="onPlayerReady" | 6 | + crossorigin="anonymous" |
| 7 | - controls | 7 | + playsinline |
| 8 | + :class="['video-player', 'vjs-big-play-centered', { loading: !state }]" | ||
| 9 | + @mounted="handleMounted" | ||
| 8 | /> | 10 | /> |
| 9 | </div> | 11 | </div> |
| 10 | </template> | 12 | </template> |
| ... | @@ -29,7 +31,8 @@ const props = defineProps({ | ... | @@ -29,7 +31,8 @@ const props = defineProps({ |
| 29 | 31 | ||
| 30 | const emit = defineEmits(['onPlay', 'onPause']) | 32 | const emit = defineEmits(['onPlay', 'onPause']) |
| 31 | const videoRef = ref(null) | 33 | const videoRef = ref(null) |
| 32 | -let player = null | 34 | +const player = ref(null) |
| 35 | +const state = ref(null) | ||
| 33 | 36 | ||
| 34 | const videoOptions = computed(() => ({ | 37 | const videoOptions = computed(() => ({ |
| 35 | fluid: true, | 38 | fluid: true, |
| ... | @@ -43,6 +46,19 @@ const videoOptions = computed(() => ({ | ... | @@ -43,6 +46,19 @@ const videoOptions = computed(() => ({ |
| 43 | }], | 46 | }], |
| 44 | onPlay: () => emit('onPlay'), | 47 | onPlay: () => emit('onPlay'), |
| 45 | onPause: () => emit('onPause'), | 48 | onPause: () => emit('onPause'), |
| 49 | + userActions: { | ||
| 50 | + hotkeys: true, | ||
| 51 | + doubleClick: true | ||
| 52 | + }, | ||
| 53 | + controlBar: { | ||
| 54 | + progressControl: { | ||
| 55 | + seekBar: { | ||
| 56 | + mouseTimeDisplay: { | ||
| 57 | + keepTooltipsInside: true | ||
| 58 | + } | ||
| 59 | + } | ||
| 60 | + } | ||
| 61 | + }, | ||
| 46 | ...props.options | 62 | ...props.options |
| 47 | })) | 63 | })) |
| 48 | 64 | ||
| ... | @@ -50,16 +66,22 @@ const onPlayerReady = (instance) => { | ... | @@ -50,16 +66,22 @@ const onPlayerReady = (instance) => { |
| 50 | player = instance | 66 | player = instance |
| 51 | } | 67 | } |
| 52 | 68 | ||
| 69 | +const handleMounted = (payload) => { | ||
| 70 | + console.log('Advanced player mounted', payload) | ||
| 71 | + state.value = payload.state | ||
| 72 | + player.value = payload.player | ||
| 73 | +} | ||
| 74 | + | ||
| 53 | onBeforeUnmount(() => { | 75 | onBeforeUnmount(() => { |
| 54 | - if (player) { | 76 | + if (videoRef.value?.$player) { |
| 55 | - player.dispose() | 77 | + videoRef.value.$player.dispose() |
| 56 | } | 78 | } |
| 57 | }) | 79 | }) |
| 58 | 80 | ||
| 59 | defineExpose({ | 81 | defineExpose({ |
| 60 | pause() { | 82 | pause() { |
| 61 | - if (player) { | 83 | + if (videoRef.value?.$player) { |
| 62 | - player.pause() | 84 | + videoRef.value.$player.pause() |
| 63 | } | 85 | } |
| 64 | } | 86 | } |
| 65 | }) | 87 | }) | ... | ... |
-
Please register or login to post a comment