diff --git a/src/audioManager.js b/src/audioManager.js index 1e99365..22483c1 100644 --- a/src/audioManager.js +++ b/src/audioManager.js @@ -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); } } diff --git a/src/components/ShootProgress.vue b/src/components/ShootProgress.vue index 1cc4737..3abf0c7 100644 --- a/src/components/ShootProgress.vue +++ b/src/components/ShootProgress.vue @@ -172,12 +172,12 @@ async function onReceiveMessage(messages = []) { wait.value = msg.wait; if (msg.wait === 20) { halfTime.value = true; - audioManager.play("中场休息"); + audioManager.play("中场休息", false); } if (msg.wait === 0) { halfTime.value = false; } - }, 500); + }, 200); } } else if (msg.constructor === MESSAGETYPES.MatchOver) { audioManager.play("比赛结束"); diff --git a/src/pages/practise-one.vue b/src/pages/practise-one.vue index 23ad8f9..07c8e6a 100644 --- a/src/pages/practise-one.vue +++ b/src/pages/practise-one.vue @@ -1,5 +1,6 @@