From a1942697e721513002b78d840f7792f666b3b32a Mon Sep 17 00:00:00 2001 From: kron Date: Fri, 29 Aug 2025 10:41:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=9F=B3=E9=A2=91=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E5=A4=B1=E8=B4=A5=E9=87=8D=E6=96=B0=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/audioManager.js | 156 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 126 insertions(+), 30 deletions(-) diff --git a/src/audioManager.js b/src/audioManager.js index 6a252a0..6576c45 100644 --- a/src/audioManager.js +++ b/src/audioManager.js @@ -55,39 +55,90 @@ class AudioManager { constructor() { this.audioMap = new Map(); this.currentPlayingKey = null; // 记录当前播放的音频key + this.retryCount = new Map(); // 记录每个音频的重试次数 + this.maxRetries = 3; // 最大重试次数 + this.retryDelay = 1000; // 重试延迟时间(毫秒) + this.initAudios(); + } + + // 初始化音频 + initAudios() { Object.keys(audioFils).forEach((key) => { - const audio = uni.createInnerAudioContext(); - audio.src = audioFils[key]; - audio.autoplay = false; - - // 监听加载状态 - audio.onCanplay(() => { - // console.log(`音频 ${key} 已加载完成`); - }); - - audio.onError((res) => { - console.log(`音频 ${key} 加载失败:`, res.errMsg); - }); - - // 监听播放结束事件 - audio.onEnded(() => { - if (this.currentPlayingKey === key) { - this.currentPlayingKey = null; - } - }); - - // 监听播放停止事件 - audio.onStop(() => { - if (this.currentPlayingKey === key) { - this.currentPlayingKey = null; - } - }); - - this.audioMap.set(key, audio); + this.createAudio(key); }); } + // 创建单个音频实例 + createAudio(key) { + const audio = uni.createInnerAudioContext(); + audio.src = audioFils[key]; + audio.autoplay = false; + + // 监听加载状态 + audio.onCanplay(() => { + // console.log(`音频 ${key} 已加载完成`); + // 加载成功后重置重试计数 + this.retryCount.set(key, 0); + }); + + audio.onError((res) => { + console.log(`音频 ${key} 加载失败:`, res.errMsg); + this.handleAudioError(key); + }); + + // 监听播放结束事件 + audio.onEnded(() => { + if (this.currentPlayingKey === key) { + this.currentPlayingKey = null; + } + }); + + // 监听播放停止事件 + audio.onStop(() => { + if (this.currentPlayingKey === key) { + this.currentPlayingKey = null; + } + }); + + this.audioMap.set(key, audio); + // 初始化重试计数 + if (!this.retryCount.has(key)) { + this.retryCount.set(key, 0); + } + } + + // 处理音频加载错误 + handleAudioError(key) { + const currentRetries = this.retryCount.get(key) || 0; + + if (currentRetries < this.maxRetries) { + // 增加重试计数 + this.retryCount.set(key, currentRetries + 1); + + console.log(`音频 ${key} 开始第 ${currentRetries + 1} 次重试...`); + + // 延迟重试 + setTimeout(() => { + this.retryLoadAudio(key); + }, this.retryDelay); + } else { + console.error(`音频 ${key} 重试 ${this.maxRetries} 次后仍然失败,停止重试`); + } + } + + // 重新加载音频 + retryLoadAudio(key) { + // 销毁旧的音频实例 + const oldAudio = this.audioMap.get(key); + if (oldAudio) { + oldAudio.destroy(); + } + + // 创建新的音频实例 + this.createAudio(key); + } + // 播放指定音频 play(key) { // 如果有正在播放的音频,先停止 @@ -97,8 +148,23 @@ class AudioManager { const audio = this.audioMap.get(key); if (audio) { - audio.play(); - this.currentPlayingKey = key; + // 检查音频是否加载失败过多次 + const retries = this.retryCount.get(key) || 0; + if (retries >= this.maxRetries) { + console.warn(`音频 ${key} 加载失败次数过多,尝试重新初始化`); + this.retryLoadAudio(key); + // 延迟播放,等待重新加载 + setTimeout(() => { + const newAudio = this.audioMap.get(key); + if (newAudio) { + newAudio.play(); + this.currentPlayingKey = key; + } + }, 500); + } else { + audio.play(); + this.currentPlayingKey = key; + } } } @@ -113,12 +179,42 @@ class AudioManager { } } + // 停止所有音频 + stopAll() { + this.audioMap.forEach((audio, key) => { + audio.stop(); + }); + this.currentPlayingKey = null; + } + + // 手动重新加载指定音频 + reloadAudio(key) { + if (audioFils[key]) { + console.log(`手动重新加载音频: ${key}`); + this.retryCount.set(key, 0); // 重置重试计数 + this.retryLoadAudio(key); + } + } + + // 重新加载所有音频 + reloadAllAudios() { + console.log('重新加载所有音频...'); + this.audioMap.forEach((audio, key) => { + audio.destroy(); + }); + this.audioMap.clear(); + this.retryCount.clear(); + this.currentPlayingKey = null; + this.initAudios(); + } + // 销毁所有音频实例 destroyAll() { this.audioMap.forEach((audio) => { audio.destroy(); }); this.audioMap.clear(); + this.retryCount.clear(); this.currentPlayingKey = null; } }