From a0e831fe40bbca482ad169e01c3e22a3f37747a2 Mon Sep 17 00:00:00 2001 From: huliyong <2783385703@qq.com> Date: Fri, 31 Oct 2025 01:35:56 +0800 Subject: [PATCH] =?UTF-8?q?fix(AudioControls):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E9=9F=B3=E9=A2=91=E6=92=AD=E6=94=BE=E7=BB=93=E6=9D=9F=E5=90=8E?= =?UTF-8?q?=E7=9A=84=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加音频数据有效性检查 - 重构播放结束逻辑到单独方法 - 增加错误处理和状态重置 - 添加查找下一个有效音频的功能 --- subPages/home/AudioControls.vue | 184 ++++++++++++++++++++++++++-------------- 1 file changed, 121 insertions(+), 63 deletions(-) diff --git a/subPages/home/AudioControls.vue b/subPages/home/AudioControls.vue index 7af9b55..b7c88df 100644 --- a/subPages/home/AudioControls.vue +++ b/subPages/home/AudioControls.vue @@ -1645,85 +1645,143 @@ export default { // 添加延迟确保音频状态完全清理 setTimeout(() => { + try { + // 检查音频数据有效性 + if (!this.currentPageAudios || this.currentPageAudios.length === 0) { + console.warn('🎵 onAudioEnded: 没有音频数据'); + this.isProcessingEnded = false; + return; + } - if (this.currentAudioIndex < this.currentPageAudios.length - 1) { - - let currentAudioData = this.currentPageAudios[this.currentAudioIndex]; - // 检查当前音频的 isLead 状态 - if (currentAudioData && !currentAudioData.isLead) { - console.log('🎵 playAudio: 当前音频 isLead=false,检查是否需要跳过 isLead=true 的音频'); + if (this.currentAudioIndex < this.currentPageAudios.length - 1) { + // 还有下一个音频,继续播放 + let currentAudioData = this.currentPageAudios[this.currentAudioIndex]; - // 从当前索引开始,跳过所有 isLead=true 的音频 - let nextIndex = this.currentAudioIndex; - while (nextIndex < (this.currentPageAudios.length - 1)) { - const audioData = this.currentPageAudios[nextIndex + 1]; - if (audioData && audioData.isLead == true) { - console.log(`🎵 playAudio: 跳过 isLead=true 的音频,索引=${nextIndex}`); - nextIndex++; - } else { - break; + // 检查当前音频的 isLead 状态 + if (currentAudioData && !currentAudioData.isLead) { + console.log('🎵 onAudioEnded: 当前音频 isLead=false,检查是否需要跳过 isLead=true 的音频'); + + // 从当前索引开始,跳过所有 isLead=true 的音频 + let nextIndex = this.currentAudioIndex; + while (nextIndex < (this.currentPageAudios.length - 1)) { + const audioData = this.currentPageAudios[nextIndex + 1]; + if (audioData && audioData.isLead == true) { + console.log(`🎵 onAudioEnded: 跳过 isLead=true 的音频,索引=${nextIndex + 1}`); + nextIndex++; + } else { + break; + } } - } - - // 更新当前音频索引 - if (nextIndex !== this.currentAudioIndex) { - this.currentAudioIndex = nextIndex; - console.log(`🎵 playAudio: 跳过后的新索引=${this.currentAudioIndex}`); - // 检查新索引是否有效 - if (this.currentAudioIndex >= this.currentPageAudios.length) { - console.log('🎵 playAudio: 跳过后已到达音频列表末尾'); - return; + // 更新当前音频索引 + if (nextIndex !== this.currentAudioIndex) { + this.currentAudioIndex = nextIndex; + console.log(`🎵 onAudioEnded: 跳过后的新索引=${this.currentAudioIndex}`); + + // 检查新索引是否有效 + if (this.currentAudioIndex >= this.currentPageAudios.length - 1) { + console.log('🎵 onAudioEnded: 跳过后已到达音频列表末尾,进入播放完毕逻辑'); + // 跳转到播放完毕的逻辑 + this.handlePlaybackComplete(); + return; + } } } - } - // 播放下一个音频 - this.currentAudioIndex++; - console.log(`🎵 onAudioEnded: 准备播放下一个音频,索引=${this.currentAudioIndex}`, this.currentPageAudios); - - // 确保音频数据有效 - const nextAudio = this.currentPageAudios[this.currentAudioIndex]; - if (nextAudio && nextAudio.url) { - this.playAudio(); - } else { - console.error('🎵 onAudioEnded: 下一个音频数据无效', nextAudio); - } + // 移动到下一个音频索引 + this.currentAudioIndex++; + + // 确保索引在有效范围内 + if (this.currentAudioIndex >= this.currentPageAudios.length) { + console.log('🎵 onAudioEnded: 索引超出范围,进入播放完毕逻辑'); + this.handlePlaybackComplete(); + return; + } - // 滚动到下一段音频对应的文字 - // setTimeout(() => { - // this.scrollToCurrentAudio(); - // }, 300); // 延迟300ms确保音频切换完成 + console.log(`🎵 onAudioEnded: 准备播放下一个音频,索引=${this.currentAudioIndex}`); - } else { - // 所有音频播放完毕 - console.log('🎵 onAudioEnded: 所有音频播放完毕'); - if (this.isLoop) { - // 循环播放 - this.currentAudioIndex = 0; - console.log('🎵 onAudioEnded: 循环播放,重置索引为0'); - this.playAudio(); - - // 滚动到第一段音频对应的文字 - // setTimeout(() => { - // this.scrollToCurrentAudio(); - // }, 300); + // 确保音频数据有效 + const nextAudio = this.currentPageAudios[this.currentAudioIndex]; + if (nextAudio && nextAudio.url) { + this.playAudio(); + } else { + console.error('🎵 onAudioEnded: 下一个音频数据无效', nextAudio); + // 如果下一个音频无效,尝试播放再下一个 + this.findAndPlayNextValidAudio(); + } } else { - // 停止播放 - this.isPlaying = false; - this.currentTime = this.totalTime; - this.currentHighlightIndex = -1; - this.$emit('highlight-change', -1); - console.log('🎵 onAudioEnded: 播放完毕,停止播放'); + // 所有音频播放完毕 + this.handlePlaybackComplete(); } + } catch (error) { + console.error('🎵 onAudioEnded: 处理过程中发生错误', error); + } finally { + // 重置防抖标志 + this.isProcessingEnded = false; } - - // 重置防抖标志 - this.isProcessingEnded = false; }, 50); // 添加50ms延迟确保状态清理完成 }, + // 处理播放完毕的逻辑 + handlePlaybackComplete() { + console.log('🎵 handlePlaybackComplete: 所有音频播放完毕'); + + if (this.isLoop) { + // 循环播放 + console.log('🎵 handlePlaybackComplete: 循环播放,重置索引为0'); + this.currentAudioIndex = 0; + + // 确保第一个音频有效 + const firstAudio = this.currentPageAudios[0]; + if (firstAudio && firstAudio.url) { + this.playAudio(); + } else { + console.error('🎵 handlePlaybackComplete: 第一个音频数据无效,停止循环播放'); + this.stopPlayback(); + } + } else { + // 停止播放 + this.stopPlayback(); + } + }, + + // 停止播放的逻辑 + stopPlayback() { + console.log('🎵 stopPlayback: 播放完毕,停止播放'); + this.isPlaying = false; + this.currentTime = this.totalTime; + this.currentHighlightIndex = -1; + this.$emit('highlight-change', -1); + + // 通知父组件音频状态变化 + this.$emit('audio-state-change', { + hasAudioData: this.hasAudioData, + isLoading: false, + currentHighlightIndex: -1 + }); + }, + + // 查找并播放下一个有效的音频 + findAndPlayNextValidAudio() { + console.log('🎵 findAndPlayNextValidAudio: 查找下一个有效音频'); + + // 从当前索引开始查找有效音频 + for (let i = this.currentAudioIndex + 1; i < this.currentPageAudios.length; i++) { + const audio = this.currentPageAudios[i]; + if (audio && audio.url) { + console.log(`🎵 findAndPlayNextValidAudio: 找到有效音频,索引=${i}`); + this.currentAudioIndex = i; + this.playAudio(); + return; + } + } + + // 没有找到有效音频,播放完毕 + console.log('🎵 findAndPlayNextValidAudio: 没有找到有效音频,播放完毕'); + this.handlePlaybackComplete(); + }, + // 滚动到当前播放音频对应的文字 // scrollToCurrentAudio() { // try {