fix:修复语音,射箭数字,第一轮提前报靶问题

This commit is contained in:
2026-05-22 17:57:20 +08:00
parent ef2a71f793
commit fe8b38bc6f
4 changed files with 91 additions and 13 deletions

View File

@@ -1,3 +1,6 @@
export const AUDIO_INTERRUPTION_BEGIN_EVENT = "audio-interruption-begin";
export const AUDIO_INTERRUPTION_END_EVENT = "audio-interruption-end";
export const audioFils = {
tententen: "https://static.shelingxingqiu.com/shootmini/static/audio/tententen.mp3",
点击按钮: "https://static.shelingxingqiu.com/shootmini/static/audio/%E7%82%B9%E5%87%BB%E6%8C%89%E9%92%AE.mp3",
@@ -97,7 +100,7 @@ function debugLog(...args) {
const envVersion = accountInfo.miniProgram.envVersion;
// 只在体验版打印日志,正式版(release)和开发版(develop)不打印
if (envVersion === "trial") {
if (envVersion === "trial" || envVersion === "develop") {
console.log(...args);
}
}
@@ -127,6 +130,7 @@ class AudioManager {
// 防重复播放保护
this.lastPlayKey = null;
this.lastPlayAt = 0;
this.isInterrupted = false;
// 静音开关
this.isMuted = false;
@@ -141,10 +145,41 @@ class AudioManager {
this.localFileCache = uni.getStorageSync("audio_local_files") || {};
// 启动时自动清理过期的缓存文件URL 已不在 audioFils 中的文件)
this.cleanObsoleteCache();
this.bindAudioInterruptionEvents();
this.initAudios();
}
bindAudioInterruptionEvents() {
if (this._audioInterruptionBound) return;
this._audioInterruptionBound = true;
const begin = () => {
if (this.isInterrupted) return;
this.isInterrupted = true;
this.stopAll();
this.isSequenceRunning = false;
this.sequenceQueue = [];
this.sequenceIndex = 0;
this.pendingPlayKey = null;
uni.$emit(AUDIO_INTERRUPTION_BEGIN_EVENT);
};
const end = () => {
if (!this.isInterrupted) return;
this.isInterrupted = false;
uni.$emit(AUDIO_INTERRUPTION_END_EVENT);
void this.reloadAll();
};
if (typeof uni?.onAudioInterruptionBegin === "function") {
uni.onAudioInterruptionBegin(begin);
}
if (typeof uni?.onAudioInterruptionEnd === "function") {
uni.onAudioInterruptionEnd(end);
}
}
// 清理不再使用的缓存文件
cleanObsoleteCache() {
const activeUrls = new Set(Object.values(audioFils));
@@ -461,6 +496,10 @@ class AudioManager {
// 播放指定音频或音频数组(数组则按顺序连续播放)
play(input, interrupt = true) {
if (this.isInterrupted) {
debugLog("音频处理中断状态,忽略播放请求");
return;
}
// 统一规范化为队列
let queue = [];
if (Array.isArray(input)) {
@@ -514,6 +553,10 @@ class AudioManager {
// 内部方法:播放单个 key
_playSingle(key, forceStopAll = false) {
if (this.isInterrupted) {
debugLog(`音频处理中断状态,跳过播放: ${key}`);
return;
}
// 200ms 内的同 key 重复播放直接忽略,避免“比比赛开始”这类重复首音
const now = Date.now();
if (this.lastPlayKey === key && now - this.lastPlayAt < 250) {
@@ -557,7 +600,13 @@ class AudioManager {
// 显式授权播放并立即播放
this.allowPlayMap.set(key, true);
audio.play();
try {
audio.play();
} catch (err) {
this.allowPlayMap.set(key, false);
debugLog(`音频 ${key} 播放调用失败`, err?.errMsg || err);
return;
}
this.currentPlayingKey = key;
this.lastPlayKey = key;
this.lastPlayAt = Date.now();