添加音频loading页面

This commit is contained in:
kron
2025-11-25 15:02:50 +08:00
parent 1320519e90
commit 15ee4e7afa
3 changed files with 134 additions and 1 deletions

View File

@@ -123,6 +123,8 @@ class AudioManager {
// 网络状态相关
this.networkOnline = true;
this.pendingPlayKey = null;
// 新增:就绪状态映射
this.readyMap = new Map();
try {
uni.onNetworkStatusChange(({ isConnected }) => {
this.networkOnline = !!isConnected;
@@ -224,6 +226,8 @@ class AudioManager {
} catch (_) {}
}
clearTimeout(loadTimeout);
// 标记为已就绪
this.readyMap.set(key, true);
debugLog(`音频 ${key} 已加载完成`);
uni.$emit("audioLoaded", key);
const loadedAudioKeys = uni.getStorageSync("loadedAudioKeys") || {};
@@ -237,6 +241,8 @@ class AudioManager {
clearTimeout(loadTimeout);
debugLog(`音频 ${key} 加载失败:`, res.errMsg);
this.allowPlayMap.set(key, false);
// 标记为未就绪
this.readyMap.set(key, false);
this.handleAudioError(key);
if (callback) callback();
});
@@ -476,6 +482,7 @@ class AudioManager {
this.allowPlayMap.delete(k);
this.retryCount.delete(k);
this.audioMap.delete(k);
this.readyMap.delete(k);
}
this.audioKeys = [];
this.currentLoadingIndex = 0;
@@ -573,7 +580,23 @@ class AudioManager {
}
debugLog(`静音状态已设置为: ${this.isMuted}`);
}
// 新增返回音频加载进度0~1
getLoadProgress() {
// 总数优先使用已初始化的 audioKeys未初始化则回退到 audioFils
const keys =
this.audioKeys && this.audioKeys.length > 0
? this.audioKeys
: Object.keys(audioFils);
const total = keys.length;
if (total === 0) return 0;
let loaded = 0;
for (const k of keys) {
if (this.readyMap.get(k)) loaded++;
}
return loaded / total;
}
}
// 导出单例
export default new AudioManager();
export default new AudioManager();