优化音频加载

This commit is contained in:
kron
2025-09-18 09:28:04 +08:00
parent aa6bbf6fd6
commit b952ea9fd0

View File

@@ -1,98 +1,146 @@
const audioFils = {
胜利: "https://static.shelingxingqiu.com/attachment/2025-09-15/dcsz3389qp0vph9zjl.mp3",
失败: "https://static.shelingxingqiu.com/attachment/2025-09-15/dcsz33746j2laokpdx.mp3",
胜利: "https://static.shelingxingqiu.com/attachment/2025-09-17/dcuo9yjp0kt5msvmvd.mp3",
失败: "https://static.shelingxingqiu.com/attachment/2025-09-17/dcuo9yht2sdwhuqygy.mp3",
请射箭测试距离:
"https://static.shelingxingqiu.com/attachment/2025-09-15/dcszloz51yo7agju3z.mp3",
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcuvj8avzqyw4hpq7t.mp3",
距离合格:
"https://static.shelingxingqiu.com/attachment/2025-09-15/dcszloz55jzwupxdfd.mp3",
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutwrda0amn5kqr4j.mp3",
距离不足:
"https://static.shelingxingqiu.com/attachment/2025-09-15/dcszloz2l6rikmbjlj.mp3",
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutya57qurnsj6pg4.mp3",
轮到你了:
"https://static.shelingxingqiu.com/attachment/2025-09-15/dcszv5hsbhdvjroggf.mp3",
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutzdrn4lxcpv8aqr.mp3",
第一轮:
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cwf81d9ibgwvj.mp3",
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutyyr9a7m1vz2w13.mp3",
第二轮:
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cvasp6fca4utl.mp3",
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutyyr9ldnfexjxtw.mp3",
第三轮:
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cvws0a45n43yd.mp3",
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutyyr97m4ipxaze4.mp3",
第四轮:
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cwfjy508hllbe.mp3",
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutyyr9x5addohlzf.mp3",
第五轮:
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cvpctfwjzkqgf.mp3",
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutyyr9d7lw2gebpv.mp3",
决金箭轮:
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cvuumxo4jybpr.mp3",
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutwrd9zs4oi2kujv.mp3",
请蓝方射箭:
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cwg1k059xeiya.mp3",
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutzdrxcbe5ll46as.mp3",
请红方射箭:
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cwfhm13t1paxt.mp3",
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutzdrl3re3dhlfjd.mp3",
中场休息:
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct0c3qxmht5ypubwm.mp3",
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutwrd9zdk1xyolst.mp3",
比赛结束:
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct1uodgi5i98q41ht.mp3",
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutya59b6pu0ur4um.mp3",
比赛开始:
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct1uoddlg85tmo9bc.mp3",
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcuu5z3a3lumkutske.mp3",
请开始射击:
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct1uod2ol85bptsth.mp3",
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutzdrl5u0iromqhf.mp3",
射击无效:
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct1uocy6y4ipqzmum.mp3",
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutya55ufiiw8oo55.mp3",
未上靶:
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cw7mqlmloxf0l.mp3",
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcuuznjc78ljhzuw1o.mp3",
"1环":
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cw3qvjznfzaof.mp3",
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutxin1aq7gxjih5l.mp3",
"2环":
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cwb0of0vb3cje.mp3",
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutxin64tdgx2s4at.mp3",
"3环":
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cwd9o2zv1qmbi.mp3",
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutxinlmf87vt8z65.mp3",
"4环":
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cwc499rhc5k9r.mp3",
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutxinniv97sx0q9u.mp3",
"5环":
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cw2ruyvchecw2.mp3",
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutxin7j01kknpb7k.mp3",
"6环":
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cw50qqkfvhmpe.mp3",
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutxin4syy1015rtq.mp3",
"7环":
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cwbgkprfhognw.mp3",
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutxin3iz3dvmjdai.mp3",
"8环":
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cweula94nxwna.mp3",
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutxinnjd42lhpfiw.mp3",
"9环":
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cwdc7bibtk3ze.mp3",
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutxin69nj1xh7yfz.mp3",
"10环":
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cwdq83u3fadj9.mp3",
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutxinnvsx0tt7ksa.mp3",
};
class AudioManager {
constructor() {
this.audioMap = new Map();
this.currentPlayingKey = null; // 记录当前播放的音频key
this.retryCount = new Map(); // 记录每个音频的重试次数
this.maxRetries = 3; // 最大重试次数
this.retryDelay = 1000; // 重试延迟时间(毫秒)
this.currentPlayingKey = null;
this.retryCount = new Map();
this.maxRetries = 3;
// 串行加载相关属性
this.audioKeys = [];
this.currentLoadingIndex = 0;
this.isLoading = false;
this.loadingPromise = null;
this.initAudios();
}
// 初始化音频
initAudios() {
Object.keys(audioFils).forEach((key) => {
this.createAudio(key);
if (this.isLoading) {
console.log("音频正在加载中,跳过重复初始化");
return this.loadingPromise;
}
console.log("开始串行加载音频...");
this.isLoading = true;
this.audioKeys = Object.keys(audioFils);
this.currentLoadingIndex = 0;
this.loadingPromise = new Promise((resolve) => {
this.loadNextAudio(resolve);
});
return this.loadingPromise;
}
// 串行加载下一个音频
loadNextAudio(onComplete) {
if (this.currentLoadingIndex >= this.audioKeys.length) {
console.log("所有音频加载完成");
this.isLoading = false;
if (onComplete) onComplete();
return;
}
const key = this.audioKeys[this.currentLoadingIndex];
console.log(`开始加载音频 ${this.currentLoadingIndex + 1}/${this.audioKeys.length}: ${key}`);
this.createAudio(key, () => {
this.currentLoadingIndex++;
setTimeout(() => {
this.loadNextAudio(onComplete);
}, 100);
});
}
// 创建单个音频实例
createAudio(key) {
createAudio(key, callback) {
const src = audioFils[key];
const audio = uni.createInnerAudioContext();
audio.src = audioFils[key];
audio.src = src;
audio.autoplay = false;
// 设置加载超时
const loadTimeout = setTimeout(() => {
console.log(`音频 ${key} 加载超时`);
audio.destroy();
if (callback) callback();
}, 10000);
// 监听加载状态
audio.onCanplay(() => {
// console.log(`音频 ${key} 已加载完成`);
// 加载成功后重置重试计数
clearTimeout(loadTimeout);
console.log(`音频 ${key} 已加载完成`);
this.retryCount.set(key, 0);
if (callback) callback();
});
audio.onError((res) => {
clearTimeout(loadTimeout);
console.log(`音频 ${key} 加载失败:`, res.errMsg);
this.handleAudioError(key);
if (callback) callback();
});
// 监听播放结束事件
@@ -110,7 +158,6 @@ class AudioManager {
});
this.audioMap.set(key, audio);
// 初始化重试计数
if (!this.retryCount.has(key)) {
this.retryCount.set(key, 0);
}
@@ -121,31 +168,28 @@ class AudioManager {
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);
}, 1000);
} else {
console.error(
`音频 ${key} 重试 ${this.maxRetries} 次后仍然失败,停止重试`
);
console.error(`音频 ${key} 重试 ${this.maxRetries} 次后仍然失败,停止重试`);
const failedAudio = this.audioMap.get(key);
if (failedAudio) {
failedAudio.destroy();
this.audioMap.delete(key);
}
}
}
// 重新加载音频
retryLoadAudio(key) {
// 销毁旧的音频实例
const oldAudio = this.audioMap.get(key);
if (oldAudio) {
oldAudio.destroy();
}
// 创建新的音频实例
this.createAudio(key);
}
@@ -158,23 +202,11 @@ class AudioManager {
const audio = this.audioMap.get(key);
if (audio) {
// 检查音频是否加载失败过多次
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;
}
audio.play();
this.currentPlayingKey = key;
} else {
console.log(`音频 ${key} 不存在,尝试重新加载...`);
this.reloadAudio(key);
}
}
@@ -189,44 +221,14 @@ 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.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;
}
}
// 导出单例