feat(播放器): 添加播放进度同步功能

在播放器组件中添加了对播放进度的监听和同步功能,确保进度条与播放时间保持一致。同时,在播放队列存储中添加了`updatedCurrentTime`字段,用于更新播放时间。这些改动提升了用户体验,使播放进度控制更加直观和准确。
This commit is contained in:
Astrian Zheng 2025-05-25 10:38:13 +10:00
parent c24d51ea9e
commit de71db1992
Signed by: Astrian
SSH Key Fingerprint: SHA256:rVnhx3DAKjujCwWE13aDl7uV6+9U1MvydLkNRXJrBiA
3 changed files with 24 additions and 4 deletions

View File

@ -44,6 +44,12 @@ function setMetadata() {
playQueueStore.duration = player.value?.duration?? 0 playQueueStore.duration = player.value?.duration?? 0
playQueueStore.currentTime = player.value?.currentTime?? 0 playQueueStore.currentTime = player.value?.currentTime?? 0
} }
watch(() => playQueueStore.updatedCurrentTime, (newValue) => {
if (!newValue) { return }
if (player.value) player.value.currentTime = newValue
playQueueStore.updatedCurrentTime = null
})
} }
function playNext() { function playNext() {

View File

@ -5,7 +5,7 @@ import gsap from 'gsap'
import { Draggable } from "gsap/Draggable" import { Draggable } from "gsap/Draggable"
import { onMounted } from 'vue' import { onMounted } from 'vue'
import { useTemplateRef } from 'vue' import { useTemplateRef } from 'vue'
import { ref } from 'vue' import { ref, watch } from 'vue'
const playQueueStore = usePlayQueueStore() const playQueueStore = usePlayQueueStore()
gsap.registerPlugin(Draggable) gsap.registerPlugin(Draggable)
@ -18,7 +18,13 @@ const displayTimeLeft = ref(false)
onMounted(() => { onMounted(() => {
Draggable.create(progressBarThumb.value, { Draggable.create(progressBarThumb.value, {
type: 'x', type: 'x',
bounds: progressBarContainer.value bounds: progressBarContainer.value,
onDrag: function() {
const thumbPosition = this.x
const containerWidth = progressBarContainer.value?.clientWidth || 0
const newTime = (thumbPosition / containerWidth) * playQueueStore.duration
playQueueStore.updatedCurrentTime = newTime
}
}) })
}) })
@ -31,7 +37,14 @@ function timeFormatter(time: number) {
return `${minutes}:${seconds < 10 ? '0' : ''}${seconds}` return `${minutes}:${seconds < 10 ? '0' : ''}${seconds}`
} }
//
watch(() => playQueueStore.currentTime, () => {
const progress = playQueueStore.currentTime / playQueueStore.duration
const containerWidth = progressBarContainer.value?.clientWidth || 0
const thumbWidth = progressBarThumb.value?.clientWidth || 0
const newPosition = (containerWidth - thumbWidth) * progress
gsap.to(progressBarThumb.value, { x: newPosition, duration: 0.1 })
})
</script> </script>
<template> <template>

View File

@ -9,6 +9,7 @@ export const usePlayQueueStore = defineStore('queue', () =>{
const isBuffering = ref<boolean>(false) const isBuffering = ref<boolean>(false)
const currentTime = ref<number>(0) const currentTime = ref<number>(0)
const duration = ref<number>(0) const duration = ref<number>(0)
const updatedCurrentTime = ref<number | null>(null)
return { list, currentIndex, isPlaying, queueReplaceLock, isBuffering, currentTime, duration } return { list, currentIndex, isPlaying, queueReplaceLock, isBuffering, currentTime, duration, updatedCurrentTime }
}) })