Files
shoot-miniprograms/src/audioManager.js

234 lines
7.1 KiB
JavaScript
Raw Normal View History

2025-07-14 16:37:56 +08:00
const audioFils = {
2025-09-15 11:23:22 +08:00
胜利: "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-15/dcszloz51yo7agju3z.mp3",
距离合格:
"https://static.shelingxingqiu.com/attachment/2025-09-15/dcszloz55jzwupxdfd.mp3",
距离不足:
"https://static.shelingxingqiu.com/attachment/2025-09-15/dcszloz2l6rikmbjlj.mp3",
轮到你了:
"https://static.shelingxingqiu.com/attachment/2025-09-15/dcszv5hsbhdvjroggf.mp3",
2025-07-14 16:37:56 +08:00
第一轮:
2025-09-15 11:23:22 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cwf81d9ibgwvj.mp3",
2025-07-14 16:37:56 +08:00
第二轮:
2025-09-15 11:23:22 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cvasp6fca4utl.mp3",
2025-07-14 16:37:56 +08:00
第三轮:
2025-09-15 11:23:22 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cvws0a45n43yd.mp3",
2025-07-14 16:37:56 +08:00
第四轮:
2025-09-15 11:23:22 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cwfjy508hllbe.mp3",
2025-07-14 16:37:56 +08:00
第五轮:
2025-09-15 11:23:22 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cvpctfwjzkqgf.mp3",
2025-07-14 16:37:56 +08:00
决金箭轮:
2025-09-15 11:23:22 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cvuumxo4jybpr.mp3",
2025-08-28 19:34:24 +08:00
请蓝方射箭:
2025-09-15 11:23:22 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cwg1k059xeiya.mp3",
2025-08-28 19:34:24 +08:00
请红方射箭:
2025-09-15 11:23:22 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cwfhm13t1paxt.mp3",
2025-07-14 16:37:56 +08:00
中场休息:
2025-09-15 11:23:22 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct0c3qxmht5ypubwm.mp3",
2025-07-14 16:37:56 +08:00
比赛结束:
2025-09-15 11:23:22 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct1uodgi5i98q41ht.mp3",
2025-07-14 16:37:56 +08:00
比赛开始:
2025-09-15 11:23:22 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct1uoddlg85tmo9bc.mp3",
2025-07-14 16:37:56 +08:00
请开始射击:
2025-09-15 11:23:22 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct1uod2ol85bptsth.mp3",
射击无效:
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct1uocy6y4ipqzmum.mp3",
2025-07-14 16:37:56 +08:00
未上靶:
2025-09-15 11:23:22 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cw7mqlmloxf0l.mp3",
2025-07-14 16:37:56 +08:00
"1环":
2025-09-15 11:23:22 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cw3qvjznfzaof.mp3",
2025-07-14 16:37:56 +08:00
"2环":
2025-09-15 11:23:22 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cwb0of0vb3cje.mp3",
2025-07-14 16:37:56 +08:00
"3环":
2025-09-15 11:23:22 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cwd9o2zv1qmbi.mp3",
2025-07-14 16:37:56 +08:00
"4环":
2025-09-15 11:23:22 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cwc499rhc5k9r.mp3",
2025-07-14 16:37:56 +08:00
"5环":
2025-09-15 11:23:22 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cw2ruyvchecw2.mp3",
2025-07-14 16:37:56 +08:00
"6环":
2025-09-15 11:23:22 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cw50qqkfvhmpe.mp3",
2025-07-14 16:37:56 +08:00
"7环":
2025-09-15 11:23:22 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cwbgkprfhognw.mp3",
2025-07-14 16:37:56 +08:00
"8环":
2025-09-15 11:23:22 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cweula94nxwna.mp3",
2025-07-14 16:37:56 +08:00
"9环":
2025-09-15 11:23:22 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cwdc7bibtk3ze.mp3",
2025-07-14 16:37:56 +08:00
"10环":
2025-09-15 11:23:22 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-15/dct08cwdq83u3fadj9.mp3",
2025-07-14 16:37:56 +08:00
};
class AudioManager {
constructor() {
this.audioMap = new Map();
2025-08-27 18:23:59 +08:00
this.currentPlayingKey = null; // 记录当前播放的音频key
2025-08-29 10:41:25 +08:00
this.retryCount = new Map(); // 记录每个音频的重试次数
this.maxRetries = 3; // 最大重试次数
this.retryDelay = 1000; // 重试延迟时间(毫秒)
2025-08-27 18:23:59 +08:00
2025-08-29 10:41:25 +08:00
this.initAudios();
}
// 初始化音频
initAudios() {
2025-07-14 16:37:56 +08:00
Object.keys(audioFils).forEach((key) => {
2025-08-29 10:41:25 +08:00
this.createAudio(key);
2025-07-14 16:37:56 +08:00
});
}
2025-08-29 10:41:25 +08:00
// 创建单个音频实例
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;
2025-09-15 11:23:22 +08:00
2025-08-29 10:41:25 +08:00
if (currentRetries < this.maxRetries) {
// 增加重试计数
this.retryCount.set(key, currentRetries + 1);
2025-09-15 11:23:22 +08:00
2025-08-29 10:41:25 +08:00
console.log(`音频 ${key} 开始第 ${currentRetries + 1} 次重试...`);
2025-09-15 11:23:22 +08:00
2025-08-29 10:41:25 +08:00
// 延迟重试
setTimeout(() => {
this.retryLoadAudio(key);
}, this.retryDelay);
} else {
2025-09-15 11:23:22 +08:00
console.error(
`音频 ${key} 重试 ${this.maxRetries} 次后仍然失败,停止重试`
);
2025-08-29 10:41:25 +08:00
}
}
// 重新加载音频
retryLoadAudio(key) {
// 销毁旧的音频实例
const oldAudio = this.audioMap.get(key);
if (oldAudio) {
oldAudio.destroy();
}
2025-09-15 11:23:22 +08:00
2025-08-29 10:41:25 +08:00
// 创建新的音频实例
this.createAudio(key);
}
2025-07-14 16:37:56 +08:00
// 播放指定音频
play(key) {
2025-08-27 18:23:59 +08:00
// 如果有正在播放的音频,先停止
if (this.currentPlayingKey) {
this.stop(this.currentPlayingKey);
}
2025-07-14 16:37:56 +08:00
const audio = this.audioMap.get(key);
2025-08-27 18:23:59 +08:00
if (audio) {
2025-08-29 10:41:25 +08:00
// 检查音频是否加载失败过多次
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;
}
2025-08-27 18:23:59 +08:00
}
2025-07-14 16:37:56 +08:00
}
// 停止指定音频
stop(key) {
const audio = this.audioMap.get(key);
2025-08-27 18:23:59 +08:00
if (audio) {
audio.stop();
if (this.currentPlayingKey === key) {
this.currentPlayingKey = null;
}
}
2025-07-14 16:37:56 +08:00
}
2025-08-29 10:41:25 +08:00
// 停止所有音频
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() {
2025-09-15 11:23:22 +08:00
console.log("重新加载所有音频...");
2025-08-29 10:41:25 +08:00
this.audioMap.forEach((audio, key) => {
audio.destroy();
});
this.audioMap.clear();
this.retryCount.clear();
this.currentPlayingKey = null;
this.initAudios();
}
2025-07-14 16:37:56 +08:00
// 销毁所有音频实例
destroyAll() {
this.audioMap.forEach((audio) => {
audio.destroy();
});
this.audioMap.clear();
2025-08-29 10:41:25 +08:00
this.retryCount.clear();
2025-08-27 18:23:59 +08:00
this.currentPlayingKey = null;
2025-07-14 16:37:56 +08:00
}
}
// 导出单例
export default new AudioManager();