Browse Source

fix(AudioControls): 修复音频播放结束后的处理逻辑

- 添加音频数据有效性检查
- 重构播放结束逻辑到单独方法
- 增加错误处理和状态重置
- 添加查找下一个有效音频的功能
main
前端-胡立永 21 hours ago
parent
commit
a0e831fe40
1 changed files with 121 additions and 63 deletions
  1. +121
    -63
      subPages/home/AudioControls.vue

+ 121
- 63
subPages/home/AudioControls.vue View File

@ -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 {


Loading…
Cancel
Save