添加播放相同声音的防抖

This commit is contained in:
kron
2025-11-14 09:25:24 +08:00
parent 2fb4740752
commit c11a108f5d

View File

@@ -111,6 +111,10 @@ class AudioManager {
this.sequenceIndex = 0; this.sequenceIndex = 0;
this.isSequenceRunning = false; this.isSequenceRunning = false;
// 防重复播放保护
this.lastPlayKey = null;
this.lastPlayAt = 0;
// 静音开关 // 静音开关
this.isMuted = false; this.isMuted = false;
@@ -355,6 +359,13 @@ class AudioManager {
// 内部方法:播放单个 key // 内部方法:播放单个 key
_playSingle(key, forceStopAll = false) { _playSingle(key, forceStopAll = false) {
// 200ms 内的同 key 重复播放直接忽略,避免“比比赛开始”这类重复首音
const now = Date.now();
if (this.lastPlayKey === key && now - this.lastPlayAt < 250) {
debugLog(`忽略快速重复播放: ${key}`);
return;
}
if (!this.networkOnline) { if (!this.networkOnline) {
const audio = this.audioMap.get(key); const audio = this.audioMap.get(key);
const isReady = this.readyMap && this.readyMap.get(key); const isReady = this.readyMap && this.readyMap.get(key);
@@ -370,6 +381,9 @@ class AudioManager {
this.stopAll(); this.stopAll();
} else if (this.currentPlayingKey && this.currentPlayingKey !== key) { } else if (this.currentPlayingKey && this.currentPlayingKey !== key) {
this.stop(this.currentPlayingKey); this.stop(this.currentPlayingKey);
} else if (this.currentPlayingKey === key) {
// 同一音频正在播放:不重启,避免听到重复开头
return;
} }
const audio = this.audioMap.get(key); const audio = this.audioMap.get(key);
@@ -401,6 +415,8 @@ class AudioManager {
audio.play(); audio.play();
this.currentPlayingKey = key; this.currentPlayingKey = key;
this.lastPlayKey = key;
this.lastPlayAt = Date.now();
} else { } else {
debugLog(`音频 ${key} 不存在,尝试重新加载...`); debugLog(`音频 ${key} 不存在,尝试重新加载...`);
this.reloadAudio(key); this.reloadAudio(key);