逻辑完善

This commit is contained in:
kron
2025-11-13 21:23:45 +08:00
parent bd01b179a6
commit 2fb4740752
3 changed files with 55 additions and 25 deletions

View File

@@ -301,29 +301,55 @@ class AudioManager {
}
// 播放指定音频或音频数组(数组则按顺序连续播放)
play(input) {
// 再次调用 play打断前面所有声音与队列
this.stopAll();
this.isSequenceRunning = false;
this.sequenceQueue = [];
this.sequenceIndex = 0;
play(input, interrupt = true) {
// 统一规范化为队列
let queue = [];
if (Array.isArray(input)) {
// 过滤可播放的 key
const queue = input.filter((k) => !!audioFils[k]);
if (queue.length === 0) {
debugLog("连续播放队列为空或无效");
return;
}
queue = input.filter((k) => !!audioFils[k]);
} else if (typeof input === "string") {
queue = !!audioFils[input] ? [input] : [];
} else {
debugLog("play 参数类型无效,仅支持字符串或字符串数组");
return;
}
if (queue.length === 0) {
debugLog("连续播放队列为空或无效");
return;
}
if (interrupt) {
// 立即打断并启动新的播放序列
this.stopAll();
this.isSequenceRunning = false;
this.sequenceQueue = [];
this.sequenceIndex = 0;
this.sequenceQueue = queue;
this.sequenceIndex = 0;
this.isSequenceRunning = true;
// 开始播放队列的第一个
this._playSingle(queue[0], false);
} else if (typeof input === "string") {
this._playSingle(input, false);
return;
}
// 不打断当前播放:把新的队列加入到序列中,等待当前播放结束后衔接
if (this.currentPlayingKey) {
if (this.isSequenceRunning) {
// 已有序列在跑:直接追加
this.sequenceQueue = this.sequenceQueue.concat(queue);
} else {
// 没有序列但当前有正在播放的:以当前为序列的起点
this.isSequenceRunning = true;
this.sequenceQueue = [this.currentPlayingKey].concat(queue);
this.sequenceIndex = 0;
// 不触发 _playSingle等待当前音频自然结束后由 onAudioEnded 接管
}
} else {
debugLog("play 参数类型无效,仅支持字符串或字符串数组");
// 当前没有播放:直接启动新的序列
this.sequenceQueue = queue;
this.sequenceIndex = 0;
this.isSequenceRunning = true;
this._playSingle(queue[0], false);
}
}