hookehuyr

fix(音频播放器): 修复音频播放时meta_id传递问题

修正音频播放器组件中meta_id的传递逻辑,确保在播放事件中正确传递当前音频的meta_id。移除不再需要的getId方法,并在音频列表初始化时添加meta_id字段。
1 <!-- 1 <!--
2 * @Date: 2025-04-07 12:35:35 2 * @Date: 2025-04-07 12:35:35
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2025-06-11 11:58:22 4 + * @LastEditTime: 2025-06-11 20:21:09
5 * @FilePath: /mlaj/src/components/ui/AudioPlayer.vue 5 * @FilePath: /mlaj/src/components/ui/AudioPlayer.vue
6 * @Description: 音频播放器组件,支持播放控制、进度条调节、音量控制、播放列表等功能 6 * @Description: 音频播放器组件,支持播放控制、进度条调节、音量控制、播放列表等功能
7 --> 7 -->
...@@ -145,6 +145,7 @@ const progress = ref(0) // 播放进度 ...@@ -145,6 +145,7 @@ const progress = ref(0) // 播放进度
145 const volume = ref(100) // 音量值 145 const volume = ref(100) // 音量值
146 const isPlaylistVisible = ref(false) // 播放列表显示状态 146 const isPlaylistVisible = ref(false) // 播放列表显示状态
147 const speed = ref(1.0) // 播放速度 147 const speed = ref(1.0) // 播放速度
148 +const meta_id = ref(null) // 音频meta_id
148 149
149 // 计算属性 150 // 计算属性
150 const currentSong = computed(() => props.songs[currentIndex.value]) // 当前播放的歌曲 151 const currentSong = computed(() => props.songs[currentIndex.value]) // 当前播放的歌曲
...@@ -185,6 +186,7 @@ const loadAudio = async () => { ...@@ -185,6 +186,7 @@ const loadAudio = async () => {
185 audio.value = new Audio(currentSong.value.url) 186 audio.value = new Audio(currentSong.value.url)
186 audio.value.volume = volume.value / 100 187 audio.value.volume = volume.value / 100
187 audio.value.playbackRate = speed.value 188 audio.value.playbackRate = speed.value
189 + meta_id.value = currentSong.value.meta_id
188 190
189 // 添加事件监听 191 // 添加事件监听
190 audio.value.addEventListener('timeupdate', updateProgress) 192 audio.value.addEventListener('timeupdate', updateProgress)
...@@ -241,7 +243,7 @@ const togglePlay = async () => { ...@@ -241,7 +243,7 @@ const togglePlay = async () => {
241 if (audio.value) { 243 if (audio.value) {
242 try { 244 try {
243 await audio.value.play() 245 await audio.value.play()
244 - emit('play', audio.value) 246 + emit('play', audio.value, meta_id.value)
245 } catch (playError) { 247 } catch (playError) {
246 console.error('播放失败:', playError) 248 console.error('播放失败:', playError)
247 return 249 return
...@@ -295,7 +297,7 @@ const prevSong = async () => { ...@@ -295,7 +297,7 @@ const prevSong = async () => {
295 audio.value.volume = normalizedVolume 297 audio.value.volume = normalizedVolume
296 isPlaying.value = true 298 isPlaying.value = true
297 // 发射播放事件 299 // 发射播放事件
298 - emit('play', audio.value) 300 + emit('play', audio.value, meta_id.value)
299 } catch (playError) { 301 } catch (playError) {
300 console.error('播放上一首歌曲失败:', playError) 302 console.error('播放上一首歌曲失败:', playError)
301 isPlaying.value = false 303 isPlaying.value = false
...@@ -335,7 +337,7 @@ const nextSong = async () => { ...@@ -335,7 +337,7 @@ const nextSong = async () => {
335 audio.value.volume = normalizedVolume 337 audio.value.volume = normalizedVolume
336 isPlaying.value = true 338 isPlaying.value = true
337 // 发射播放事件 339 // 发射播放事件
338 - emit('play', audio.value) 340 + emit('play', audio.value, meta_id.value)
339 } catch (playError) { 341 } catch (playError) {
340 console.error('播放下一首歌曲失败:', playError) 342 console.error('播放下一首歌曲失败:', playError)
341 isPlaying.value = false 343 isPlaying.value = false
...@@ -384,7 +386,7 @@ watch(() => props.songs, () => { ...@@ -384,7 +386,7 @@ watch(() => props.songs, () => {
384 console.warn('歌曲列表变化', props.songs); 386 console.warn('歌曲列表变化', props.songs);
385 currentIndex.value = 0 387 currentIndex.value = 0
386 loadAudio() 388 loadAudio()
387 -}, { deep: true }) 389 +}, { deep: true, immediate: true })
388 390
389 // 组件卸载时清理事件监听 391 // 组件卸载时清理事件监听
390 onUnmounted(() => { 392 onUnmounted(() => {
...@@ -429,7 +431,7 @@ const selectSong = async (index) => { ...@@ -429,7 +431,7 @@ const selectSong = async (index) => {
429 await audio.value.play() 431 await audio.value.play()
430 isPlaying.value = true 432 isPlaying.value = true
431 // 发射播放事件 433 // 发射播放事件
432 - emit('play', audio.value) 434 + emit('play', audio.value, meta_id.value)
433 // 关闭播放列表 435 // 关闭播放列表
434 isPlaylistVisible.value = false 436 isPlaylistVisible.value = false
435 // 滚动到当前播放的音频 437 // 滚动到当前播放的音频
...@@ -481,9 +483,6 @@ defineExpose({ ...@@ -481,9 +483,6 @@ defineExpose({
481 isPlaying: () => isPlaying.value, 483 isPlaying: () => isPlaying.value,
482 id: props.id, 484 id: props.id,
483 getPlayer: () => audio.value, 485 getPlayer: () => audio.value,
484 - getId() {
485 - return currentSong.value.meta_id || 'meta_id'
486 - }
487 }) 486 })
488 </script> 487 </script>
489 488
......
...@@ -370,6 +370,7 @@ const handleLessonClick = async (lesson) => { ...@@ -370,6 +370,7 @@ const handleLessonClick = async (lesson) => {
370 // 更新音频列表数据 370 // 更新音频列表数据
371 if (data.course_type === 'audio' && data.file?.list?.length) { 371 if (data.course_type === 'audio' && data.file?.list?.length) {
372 audioList.value = data.file.list.map(item => ({ 372 audioList.value = data.file.list.map(item => ({
373 + meta_id: item.meta_id,
373 title: item.title || '未命名音频', 374 title: item.title || '未命名音频',
374 artist: '', 375 artist: '',
375 url: item.url, 376 url: item.url,
...@@ -713,10 +714,10 @@ const downloadFile = ({ title, url, meta_id }) => { ...@@ -713,10 +714,10 @@ const downloadFile = ({ title, url, meta_id }) => {
713 * 音频播放事件 714 * 音频播放事件
714 * @param audio 音频对象 715 * @param audio 音频对象
715 */ 716 */
716 -const onAudioPlay = (audio) => { 717 +const onAudioPlay = (audio, meta_id) => {
717 console.log('开始播放音频', audio); 718 console.log('开始播放音频', audio);
718 // 学习时长埋点开始 719 // 学习时长埋点开始
719 - startAction(); 720 + startAction({meta_id});
720 } 721 }
721 722
722 /** 723 /**
...@@ -787,10 +788,10 @@ const startAction = (item) => { ...@@ -787,10 +788,10 @@ const startAction = (item) => {
787 // 更新当前播放位置(如果是音频播放) 788 // 更新当前播放位置(如果是音频播放)
788 if (audioPlayerRef.value && audioPlayerRef.value.getPlayer()) { 789 if (audioPlayerRef.value && audioPlayerRef.value.getPlayer()) {
789 audioPosition.value = audioPlayerRef.value.getPlayer().currentTime; 790 audioPosition.value = audioPlayerRef.value.getPlayer().currentTime;
790 - console.log('音频总时长:', audioDuration.value, '当前位置:', audioPosition.value, 'id:', audioPlayerRef.value.getId()); 791 + console.log('音频总时长:', audioDuration.value, '当前位置:', audioPosition.value, 'id:', item?.meta_id);
791 paramsObj = { 792 paramsObj = {
792 schedule_id: courseId.value, 793 schedule_id: courseId.value,
793 - meta_id: audioPlayerRef.value.getId(), 794 + meta_id: item?.meta_id,
794 media_duration: audioDuration.value, 795 media_duration: audioDuration.value,
795 playback_position: audioPosition.value, 796 playback_position: audioPosition.value,
796 playback_id: uuid, 797 playback_id: uuid,
......