Files
shoot-miniprograms/src/audioManager.js

702 lines
23 KiB
JavaScript
Raw Normal View History

2025-11-06 10:52:08 +08:00
export const audioFils = {
2025-12-26 16:16:05 +08:00
// 激光已校准:
// "https://static.shelingxingqiu.com/attachment/2025-10-29/ddupaur1vdkyhzaqdc.mp3",
2025-09-18 09:28:04 +08:00
胜利: "https://static.shelingxingqiu.com/attachment/2025-09-17/dcuo9yjp0kt5msvmvd.mp3",
失败: "https://static.shelingxingqiu.com/attachment/2025-09-17/dcuo9yht2sdwhuqygy.mp3",
2025-09-15 11:23:22 +08:00
请射箭测试距离:
2025-09-18 09:28:04 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcuvj8avzqyw4hpq7t.mp3",
2025-09-15 11:23:22 +08:00
距离合格:
2025-09-18 09:28:04 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutwrda0amn5kqr4j.mp3",
2025-09-15 11:23:22 +08:00
距离不足:
2025-11-12 15:54:03 +08:00
"https://static.shelingxingqiu.com/attachment/2025-11-12/de6hr2faw28t0ianh0.mp3",
2025-09-15 11:23:22 +08:00
轮到你了:
2025-09-18 09:28:04 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutzdrn4lxcpv8aqr.mp3",
2025-07-14 16:37:56 +08:00
第一轮:
2025-09-18 09:28:04 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutyyr9a7m1vz2w13.mp3",
2025-07-14 16:37:56 +08:00
第二轮:
2025-09-18 09:28:04 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutyyr9ldnfexjxtw.mp3",
2025-07-14 16:37:56 +08:00
第三轮:
2025-09-18 09:28:04 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutyyr97m4ipxaze4.mp3",
2025-07-14 16:37:56 +08:00
第四轮:
2025-09-18 09:28:04 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutyyr9x5addohlzf.mp3",
2025-07-14 16:37:56 +08:00
第五轮:
2025-09-18 09:28:04 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutyyr9d7lw2gebpv.mp3",
2025-07-14 16:37:56 +08:00
决金箭轮:
2025-09-18 09:28:04 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutwrd9zs4oi2kujv.mp3",
2025-08-28 19:34:24 +08:00
请蓝方射箭:
2025-09-18 09:28:04 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutzdrxcbe5ll46as.mp3",
2025-08-28 19:34:24 +08:00
请红方射箭:
2025-09-18 09:28:04 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutzdrl3re3dhlfjd.mp3",
2025-07-14 16:37:56 +08:00
中场休息:
2025-09-18 09:28:04 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutwrd9zdk1xyolst.mp3",
2025-07-14 16:37:56 +08:00
比赛结束:
2025-09-18 09:28:04 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutya59b6pu0ur4um.mp3",
2025-07-14 16:37:56 +08:00
比赛开始:
2025-09-18 09:28:04 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcuu5z3a3lumkutske.mp3",
2025-07-14 16:37:56 +08:00
请开始射击:
2025-09-18 09:28:04 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutzdrl5u0iromqhf.mp3",
2025-09-15 11:23:22 +08:00
射击无效:
2025-09-18 09:28:04 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutya55ufiiw8oo55.mp3",
2025-07-14 16:37:56 +08:00
未上靶:
2025-11-12 20:40:00 +08:00
"https://static.shelingxingqiu.com/attachment/2025-11-12/de6n45o3tsm1v4unam.mp3",
2025-07-14 16:37:56 +08:00
"1环":
2025-09-18 09:28:04 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutxin1aq7gxjih5l.mp3",
2025-07-14 16:37:56 +08:00
"2环":
2025-09-18 09:28:04 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutxin64tdgx2s4at.mp3",
2025-07-14 16:37:56 +08:00
"3环":
2025-09-18 09:28:04 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutxinlmf87vt8z65.mp3",
2025-07-14 16:37:56 +08:00
"4环":
2025-09-18 09:28:04 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutxinniv97sx0q9u.mp3",
2025-07-14 16:37:56 +08:00
"5环":
2025-09-18 09:28:04 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutxin7j01kknpb7k.mp3",
2025-07-14 16:37:56 +08:00
"6环":
2025-09-18 09:28:04 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutxin4syy1015rtq.mp3",
2025-07-14 16:37:56 +08:00
"7环":
2025-09-18 09:28:04 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutxin3iz3dvmjdai.mp3",
2025-07-14 16:37:56 +08:00
"8环":
2025-09-18 09:28:04 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutxinnjd42lhpfiw.mp3",
2025-07-14 16:37:56 +08:00
"9环":
2025-09-18 09:28:04 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutxin69nj1xh7yfz.mp3",
2025-07-14 16:37:56 +08:00
"10环":
2025-09-18 09:28:04 +08:00
"https://static.shelingxingqiu.com/attachment/2025-09-17/dcutxinnvsx0tt7ksa.mp3",
2025-11-12 15:54:03 +08:00
向上调整:
"https://static.shelingxingqiu.com/attachment/2025-11-12/de6ellf5pfvu3l8dhr.mp3",
向右上调整:
"https://static.shelingxingqiu.com/attachment/2025-11-12/de6ellf45v88pirarr.mp3",
向右调整:
"https://static.shelingxingqiu.com/attachment/2025-11-12/de6elleqnhrenggxsb.mp3",
向右下调整:
"https://static.shelingxingqiu.com/attachment/2025-11-12/de6elleo6q16qctf6a.mp3",
向下调整:
"https://static.shelingxingqiu.com/attachment/2025-11-12/de6ellek2mu2cri2n9.mp3",
向左下调整:
"https://static.shelingxingqiu.com/attachment/2025-11-12/de6ellf25yu1pt2k5r.mp3",
向左调整:
"https://static.shelingxingqiu.com/attachment/2025-11-12/de6ellen3zoalxcb06.mp3",
向左上调整:
"https://static.shelingxingqiu.com/attachment/2025-11-12/de6ellf37a2iw6w4pu.mp3",
2025-11-13 21:04:48 +08:00
最后30秒:
"https://static.shelingxingqiu.com/attachment/2025-11-13/de7kzzllq0futwynso.mp3",
2025-11-14 15:51:45 +08:00
练习开始:
"https://static.shelingxingqiu.com/attachment/2025-11-14/de88w0lmmt43nnfmoi.mp3",
2025-07-14 16:37:56 +08:00
};
2025-09-18 10:01:10 +08:00
// 版本控制日志函数
function debugLog(...args) {
// 获取当前环境信息
const accountInfo = uni.getAccountInfoSync();
const envVersion = accountInfo.miniProgram.envVersion;
2025-10-29 18:07:42 +08:00
2025-09-18 10:01:10 +08:00
// 只在体验版打印日志,正式版(release)和开发版(develop)不打印
2025-10-29 18:07:42 +08:00
if (envVersion === "trial") {
2025-09-18 10:01:10 +08:00
console.log(...args);
}
}
2025-07-14 16:37:56 +08:00
class AudioManager {
constructor() {
this.audioMap = new Map();
2025-09-18 09:28:04 +08:00
this.currentPlayingKey = null;
this.maxRetries = 3;
2025-11-27 18:16:32 +08:00
// 多轮统一重试:最多重试的轮次与每轮间隔
this.maxRetryRounds = 10;
this.retryRoundIntervalMs = 1500;
2025-11-05 18:30:33 +08:00
// 显式授权播放标记,防止 iOS 在设置 src 后误播
this.allowPlayMap = new Map();
2025-10-29 18:07:42 +08:00
2025-09-18 09:28:04 +08:00
// 串行加载相关属性
this.audioKeys = [];
this.currentLoadingIndex = 0;
this.isLoading = false;
this.loadingPromise = null;
2025-10-29 18:07:42 +08:00
2025-11-12 11:39:17 +08:00
// 连续播放队列相关属性
this.sequenceQueue = [];
this.sequenceIndex = 0;
this.isSequenceRunning = false;
2025-11-14 09:25:24 +08:00
// 防重复播放保护
this.lastPlayKey = null;
this.lastPlayAt = 0;
2025-11-12 16:14:48 +08:00
// 静音开关
this.isMuted = false;
2025-11-06 17:38:34 +08:00
this.pendingPlayKey = null;
2025-11-25 15:02:50 +08:00
// 新增:就绪状态映射
this.readyMap = new Map();
2025-11-27 18:16:32 +08:00
// 新增:首轮失败的音频集合与重试阶段标识
this.failedLoadKeys = new Set();
2025-12-29 11:53:19 +08:00
// 加载代数,用于 reloadAll 时作废旧的加载循环
this.loadGeneration = 0;
2025-12-30 18:10:31 +08:00
// 本地路径缓存 Map: { url: localPath }
this.localFileCache = uni.getStorageSync("audio_local_files") || {};
// 启动时自动清理过期的缓存文件URL 已不在 audioFils 中的文件)
this.cleanObsoleteCache();
2026-01-06 18:13:55 +08:00
2025-08-29 10:41:25 +08:00
this.initAudios();
}
2025-12-30 18:10:31 +08:00
// 清理不再使用的缓存文件
cleanObsoleteCache() {
const activeUrls = new Set(Object.values(audioFils));
const cachedUrls = Object.keys(this.localFileCache);
let hasChanges = false;
for (const url of cachedUrls) {
if (!activeUrls.has(url)) {
debugLog(`发现废弃音频缓存,正在清理: ${url}`);
const path = this.localFileCache[url];
// 移除物理文件
uni.removeSavedFile({
filePath: path,
complete: () => {
// 忽略移除结果,直接移除记录
},
});
// 移除记录
delete this.localFileCache[url];
hasChanges = true;
}
}
if (hasChanges) {
uni.setStorageSync("audio_local_files", this.localFileCache);
debugLog("废弃缓存清理完成");
}
}
2025-11-27 18:16:32 +08:00
// 初始化音频(两阶段:首轮串行加载全部,次轮仅串行加载失败项一次)
2025-08-29 10:41:25 +08:00
initAudios() {
2025-09-18 09:28:04 +08:00
if (this.isLoading) {
2025-09-18 10:01:10 +08:00
debugLog("音频正在加载中,跳过重复初始化");
2025-09-18 09:28:04 +08:00
return this.loadingPromise;
}
2025-09-18 10:01:10 +08:00
debugLog("开始串行加载音频...");
2025-09-18 09:28:04 +08:00
this.isLoading = true;
this.audioKeys = Object.keys(audioFils);
this.currentLoadingIndex = 0;
2025-11-27 18:16:32 +08:00
this.failedLoadKeys.clear();
2025-10-29 18:07:42 +08:00
2025-12-29 11:53:19 +08:00
// 增加代数,使得旧的加载循环失效
this.loadGeneration = (this.loadGeneration || 0) + 1;
const currentGen = this.loadGeneration;
2025-09-18 09:28:04 +08:00
this.loadingPromise = new Promise((resolve) => {
2025-11-27 18:16:32 +08:00
const finalize = () => {
2025-12-29 11:53:19 +08:00
if (currentGen !== this.loadGeneration) return;
2025-11-27 18:16:32 +08:00
const runRounds = (round) => {
2025-12-29 11:53:19 +08:00
if (currentGen !== this.loadGeneration) return;
2025-11-27 18:16:32 +08:00
// 达到最大轮次或没有失败项,收尾
if (this.failedLoadKeys.size === 0 || round > this.maxRetryRounds) {
this.isLoading = false;
resolve();
return;
}
const retryKeys = Array.from(this.failedLoadKeys);
this.failedLoadKeys.clear();
debugLog(`开始第 ${round} 轮串行加载,共 ${retryKeys.length}`);
2025-12-29 11:53:19 +08:00
this.loadKeysSequentially(
retryKeys,
() => {
if (currentGen !== this.loadGeneration) return;
// 如仍有失败项,继续下一轮;否则结束
if (this.failedLoadKeys.size > 0 && round < this.maxRetryRounds) {
setTimeout(
() => runRounds(round + 1),
this.retryRoundIntervalMs
);
} else {
this.isLoading = false;
resolve();
}
},
currentGen
);
2025-11-27 18:16:32 +08:00
};
// 启动第 1 轮重试(如有失败项)
runRounds(1);
};
2025-12-29 11:53:19 +08:00
this.loadNextAudio(finalize, currentGen);
2025-09-18 09:28:04 +08:00
});
return this.loadingPromise;
}
2025-11-27 18:16:32 +08:00
// 按自定义列表串行加载音频(避免并发过多)
2025-12-29 11:53:19 +08:00
loadKeysSequentially(keys, onComplete, gen) {
if (gen !== undefined && gen !== this.loadGeneration) return;
2025-11-27 18:16:32 +08:00
let idx = 0;
const list = Array.from(keys);
const next = () => {
2025-12-29 11:53:19 +08:00
if (gen !== undefined && gen !== this.loadGeneration) return;
2025-11-27 18:16:32 +08:00
if (idx >= list.length) {
if (onComplete) onComplete();
return;
}
const k = list[idx++];
// 已就绪的音频不再重载,避免把 ready 状态重置为 false
if (this.readyMap.get(k)) {
setTimeout(next, 50);
return;
}
// 未就绪:已存在则重载;不存在则创建
if (this.audioMap.has(k)) {
this.retryLoadAudio(k);
setTimeout(next, 100);
} else {
this.createAudio(k, () => {
setTimeout(next, 100);
});
return; // createAudio 内部会触发 next
}
};
next();
}
// 串行加载下一个音频(首轮)
2025-12-29 11:53:19 +08:00
loadNextAudio(onComplete, gen) {
if (gen !== undefined && gen !== this.loadGeneration) return;
2025-09-18 09:28:04 +08:00
if (this.currentLoadingIndex >= this.audioKeys.length) {
2025-11-27 18:16:32 +08:00
debugLog("首轮加载遍历完成", this.currentLoadingIndex);
2025-09-18 09:28:04 +08:00
if (onComplete) onComplete();
return;
}
const key = this.audioKeys[this.currentLoadingIndex];
2025-10-29 18:07:42 +08:00
debugLog(
`开始加载音频 ${this.currentLoadingIndex + 1}/${
this.audioKeys.length
}: ${key}`
);
2025-09-18 09:28:04 +08:00
this.createAudio(key, () => {
setTimeout(() => {
2025-12-29 11:53:19 +08:00
this.loadNextAudio(onComplete, gen);
2025-09-18 09:28:04 +08:00
}, 100);
2025-07-14 16:37:56 +08:00
});
}
2025-12-30 18:10:31 +08:00
// 创建单个音频实例(支持本地缓存)
2025-09-18 09:28:04 +08:00
createAudio(key, callback) {
2025-11-27 18:16:32 +08:00
this.currentLoadingIndex++;
2025-09-18 09:28:04 +08:00
const src = audioFils[key];
2025-11-12 16:14:48 +08:00
2025-11-27 18:16:32 +08:00
const setupAudio = (realSrc) => {
const audio = uni.createInnerAudioContext();
audio.autoplay = false;
audio.src = realSrc;
try {
if (typeof audio.volume === "number") {
audio.volume = this.isMuted ? 0 : 1;
} else if (typeof audio.muted !== "undefined") {
audio.muted = this.isMuted;
}
} catch (_) {}
this.allowPlayMap.set(key, false);
audio.onPlay(() => {
if (!this.allowPlayMap.get(key)) {
try {
audio.stop();
} catch (_) {}
}
});
2025-11-05 18:30:33 +08:00
2025-11-27 18:16:32 +08:00
const loadTimeout = setTimeout(() => {
debugLog(`音频 ${key} 加载超时`);
this.recordLoadFailure(key);
2025-11-05 18:30:33 +08:00
try {
2025-11-27 18:16:32 +08:00
audio.destroy();
2025-11-05 18:30:33 +08:00
} catch (_) {}
2025-11-27 18:16:32 +08:00
if (callback) callback();
}, 10000);
audio.onCanplay(() => {
if (!this.allowPlayMap.get(key)) {
try {
audio.pause();
} catch (_) {}
}
clearTimeout(loadTimeout);
this.readyMap.set(key, true);
this.failedLoadKeys.delete(key);
2025-12-30 18:10:31 +08:00
// debugLog(`音频 ${key} 已加载完成`, this.getLoadProgress());
2025-11-27 18:16:32 +08:00
uni.$emit("audioLoaded", key);
const loadedAudioKeys = uni.getStorageSync("loadedAudioKeys") || {};
loadedAudioKeys[key] = true;
uni.setStorageSync("loadedAudioKeys", loadedAudioKeys);
if (callback) callback();
});
2025-08-29 10:41:25 +08:00
2025-11-27 18:16:32 +08:00
audio.onError((res) => {
clearTimeout(loadTimeout);
debugLog(`音频 ${key} 加载失败:`, res.errMsg);
2025-12-30 18:10:31 +08:00
// 如果是本地文件加载失败,可能是文件损坏,清除缓存以便下次重新下载
if (realSrc !== src && this.localFileCache[src] === realSrc) {
debugLog(`本地缓存失效,移除记录: ${key}`);
delete this.localFileCache[src];
uni.setStorageSync("audio_local_files", this.localFileCache);
// 移除文件
uni.removeSavedFile({ filePath: realSrc });
}
2025-11-27 18:16:32 +08:00
this.recordLoadFailure(key);
this.audioMap.delete(key);
audio.destroy();
if (this.readyMap.get(key)) {
2025-12-30 18:10:31 +08:00
// 这里不要去除,不然检查进度的时候由于没有重新加载而进度卡住,等播放失败的时候会重新加载
// this.readyMap.set(key, false);
2025-11-27 18:16:32 +08:00
} else {
if (callback) callback();
}
});
2025-08-29 10:41:25 +08:00
2025-11-27 18:16:32 +08:00
audio.onEnded(() => {
if (this.currentPlayingKey === key) {
this.currentPlayingKey = null;
}
this.allowPlayMap.set(key, false);
this.onAudioEnded(key);
});
2025-08-29 10:41:25 +08:00
2025-11-27 18:16:32 +08:00
audio.onStop(() => {
if (this.currentPlayingKey === key) {
this.currentPlayingKey = null;
}
this.allowPlayMap.set(key, false);
});
2025-08-29 10:41:25 +08:00
2025-11-27 18:16:32 +08:00
this.audioMap.set(key, audio);
};
2025-08-29 10:41:25 +08:00
2025-12-30 18:10:31 +08:00
// 检查是否有可用的本地缓存
this.checkLocalFile(src).then((localPath) => {
if (localPath) {
debugLog(`命中本地缓存: ${key}`);
setupAudio(localPath);
} else {
// 下载并尝试保存
uni.downloadFile({
url: src,
timeout: 20000,
success: (res) => {
if (res.tempFilePath) {
// 尝试保存文件到本地存储(持久化)
2026-01-06 18:13:55 +08:00
uni.getFileSystemManager().saveFile({
2025-12-30 18:10:31 +08:00
tempFilePath: res.tempFilePath,
success: (saveRes) => {
const savedPath = saveRes.savedFilePath;
this.localFileCache[src] = savedPath;
uni.setStorageSync("audio_local_files", this.localFileCache);
debugLog(`音频已缓存到本地: ${key}`);
setupAudio(savedPath);
},
fail: (err) => {
debugLog(
`保存音频失败(可能空间不足),使用临时文件: ${key}`,
err
);
setupAudio(res.tempFilePath);
},
});
} else {
this.recordLoadFailure(key);
if (callback) callback();
}
},
fail: () => {
this.recordLoadFailure(key);
if (callback) callback();
},
});
}
});
}
// 检查本地文件是否有效
checkLocalFile(url) {
return new Promise((resolve) => {
const path = this.localFileCache[url];
if (!path) {
resolve(null);
return;
}
// 检查文件是否存在
uni.getFileSystemManager().getFileInfo({
filePath: path,
success: () => {
resolve(path);
},
fail: () => {
// 文件不存在,清理记录
delete this.localFileCache[url];
uni.setStorageSync("audio_local_files", this.localFileCache);
resolve(null);
},
});
2025-11-27 18:16:32 +08:00
});
2025-08-29 10:41:25 +08:00
}
2025-11-27 18:16:32 +08:00
// 新增:记录失败(首轮与次轮都会用到)
recordLoadFailure(key) {
this.failedLoadKeys.add(key);
2025-08-29 10:41:25 +08:00
}
// 重新加载音频
retryLoadAudio(key) {
const oldAudio = this.audioMap.get(key);
2025-11-27 18:16:32 +08:00
if (oldAudio) oldAudio.destroy();
2025-08-29 10:41:25 +08:00
this.createAudio(key);
}
2025-11-12 11:39:17 +08:00
// 播放指定音频或音频数组(数组则按顺序连续播放)
2025-11-13 21:23:45 +08:00
play(input, interrupt = true) {
// 统一规范化为队列
let queue = [];
2025-11-12 11:39:17 +08:00
if (Array.isArray(input)) {
2025-11-13 21:23:45 +08:00
queue = input.filter((k) => !!audioFils[k]);
} else if (typeof input === "string") {
queue = !!audioFils[input] ? [input] : [];
} else {
debugLog("play 参数类型无效,仅支持字符串或字符串数组");
return;
}
if (queue.length === 0) {
debugLog("连续播放队列为空或无效");
return;
}
if (interrupt) {
// 立即打断并启动新的播放序列
this.stopAll();
this.isSequenceRunning = false;
this.sequenceQueue = [];
this.sequenceIndex = 0;
2025-11-12 11:39:17 +08:00
this.sequenceQueue = queue;
this.sequenceIndex = 0;
this.isSequenceRunning = true;
this._playSingle(queue[0], false);
2025-11-13 21:23:45 +08:00
return;
}
// 不打断当前播放:把新的队列加入到序列中,等待当前播放结束后衔接
if (this.currentPlayingKey) {
if (this.isSequenceRunning) {
// 已有序列在跑:直接追加
this.sequenceQueue = this.sequenceQueue.concat(queue);
} else {
// 没有序列但当前有正在播放的:以当前为序列的起点
this.isSequenceRunning = true;
this.sequenceQueue = [this.currentPlayingKey].concat(queue);
this.sequenceIndex = 0;
// 不触发 _playSingle等待当前音频自然结束后由 onAudioEnded 接管
}
2025-11-12 11:39:17 +08:00
} else {
2025-11-13 21:23:45 +08:00
// 当前没有播放:直接启动新的序列
this.sequenceQueue = queue;
this.sequenceIndex = 0;
this.isSequenceRunning = true;
this._playSingle(queue[0], false);
2025-11-12 11:39:17 +08:00
}
}
// 内部方法:播放单个 key
_playSingle(key, forceStopAll = false) {
2025-11-14 09:25:24 +08:00
// 200ms 内的同 key 重复播放直接忽略,避免“比比赛开始”这类重复首音
const now = Date.now();
if (this.lastPlayKey === key && now - this.lastPlayAt < 250) {
debugLog(`忽略快速重复播放: ${key}`);
return;
}
2025-11-12 11:39:17 +08:00
if (forceStopAll) {
this.stopAll();
} else if (this.currentPlayingKey && this.currentPlayingKey !== key) {
2025-08-27 18:23:59 +08:00
this.stop(this.currentPlayingKey);
2025-11-14 09:25:24 +08:00
} else if (this.currentPlayingKey === key) {
// 同一音频正在播放:不重启,避免听到重复开头
return;
2025-08-27 18:23:59 +08:00
}
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-11-12 16:14:48 +08:00
// 播放前确保遵循当前静音状态
try {
if (typeof audio.volume === "number") {
audio.volume = this.isMuted ? 0 : 1;
} else if (typeof audio.muted !== "undefined") {
audio.muted = this.isMuted;
}
} catch (_) {}
2025-11-06 11:29:08 +08:00
// 同一音频:避免 stop() 触发 onStop 清除授权,使用 pause()+seek(0)
try {
audio.pause();
} catch (_) {}
try {
if (typeof audio.seek === "function") {
audio.seek(0);
} else {
audio.startTime = 0;
}
} catch (_) {
audio.startTime = 0;
}
2025-11-05 18:30:33 +08:00
// 显式授权播放并立即播放
this.allowPlayMap.set(key, true);
2025-11-06 11:29:08 +08:00
2025-09-18 09:28:04 +08:00
audio.play();
this.currentPlayingKey = key;
2025-11-14 09:25:24 +08:00
this.lastPlayKey = key;
this.lastPlayAt = Date.now();
2025-09-18 09:28:04 +08:00
} else {
2025-09-18 10:01:10 +08:00
debugLog(`音频 ${key} 不存在,尝试重新加载...`);
2025-11-27 18:16:32 +08:00
this.retryLoadAudio(key);
const handler = (loadedKey) => {
if (loadedKey === key) {
try {
uni.$off("audioLoaded", handler);
} catch (_) {}
// 再次校验是否存在且就绪
const a = this.audioMap.get(key);
if (a && this.readyMap.get(key)) {
this._playSingle(key, false);
}
}
};
try {
uni.$on("audioLoaded", handler);
} catch (_) {}
2025-08-27 18:23:59 +08:00
}
2025-07-14 16:37:56 +08:00
}
2025-11-12 11:39:17 +08:00
// 连续播放:在某个音频结束后,若处于队列播放状态则继续下一个
onAudioEnded(key) {
if (!this.isSequenceRunning) return;
const currentKey = this.sequenceQueue[this.sequenceIndex];
if (currentKey !== key) return;
const nextIndex = this.sequenceIndex + 1;
if (nextIndex < this.sequenceQueue.length) {
this.sequenceIndex = nextIndex;
const nextKey = this.sequenceQueue[nextIndex];
this._playSingle(nextKey, false);
} else {
// 队列播放完成
this.isSequenceRunning = false;
this.sequenceQueue = [];
this.sequenceIndex = 0;
}
}
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();
2025-11-05 18:30:33 +08:00
this.allowPlayMap.set(key, false);
2025-08-27 18:23:59 +08:00
if (this.currentPlayingKey === key) {
this.currentPlayingKey = null;
}
}
2025-07-14 16:37:56 +08:00
}
2025-11-06 14:06:37 +08:00
// 停止所有音频
stopAll() {
for (const [k, audio] of this.audioMap.entries()) {
try {
audio.stop();
} catch (_) {}
this.allowPlayMap.set(k, false);
}
this.currentPlayingKey = null;
}
2025-11-12 16:14:48 +08:00
// 设置静音开关true 静音false 取消静音
setMuted(muted) {
this.isMuted = !!muted;
for (const audio of this.audioMap.values()) {
try {
if (typeof audio.volume === "number") {
audio.volume = this.isMuted ? 0 : 1;
} else if (typeof audio.muted !== "undefined") {
audio.muted = this.isMuted;
}
} catch (_) {}
}
debugLog(`静音状态已设置为: ${this.isMuted}`);
}
2025-11-25 15:02:50 +08:00
// 新增返回音频加载进度0~1
getLoadProgress() {
2025-11-27 18:16:32 +08:00
const keys = Object.keys(audioFils);
2025-11-25 15:02:50 +08:00
const total = keys.length;
if (total === 0) return 0;
let loaded = 0;
for (const k of keys) {
if (this.readyMap.get(k)) loaded++;
}
2025-11-27 18:16:32 +08:00
return Number((loaded / total).toFixed(2));
2025-11-25 15:02:50 +08:00
}
2025-12-29 11:53:19 +08:00
2025-12-30 18:10:31 +08:00
// 清理本地音频缓存文件
clearCache() {
debugLog("开始清理本地音频缓存...");
const cache = uni.getStorageSync("audio_local_files") || {};
const paths = Object.values(cache);
for (const path of paths) {
uni.removeSavedFile({
filePath: path,
complete: (res) => {
// 无论成功失败都继续
},
});
}
uni.removeStorageSync("audio_local_files");
this.localFileCache = {};
debugLog("本地音频缓存清理完成");
}
2025-12-29 11:53:19 +08:00
// 手动重置并重新加载所有音频(用于卡住时恢复)
reloadAll() {
2025-12-30 18:10:31 +08:00
debugLog("执行 reloadAll: 重置所有状态并重新加载");
2025-12-29 11:53:19 +08:00
// 1. 停止所有播放
this.stopAll();
// 2. 销毁现有音频实例
for (const audio of this.audioMap.values()) {
try {
audio.destroy();
} catch (_) {}
}
this.audioMap.clear();
// 3. 重置状态
this.readyMap.clear();
this.failedLoadKeys.clear();
this.allowPlayMap.clear();
this.currentPlayingKey = null;
this.sequenceQueue = [];
this.sequenceIndex = 0;
this.isSequenceRunning = false;
2025-12-30 18:10:31 +08:00
// 清理一下可能损坏的缓存(可选,如果用户因为缓存坏了卡住,这一步很有用)
// 这里选择不自动全清,而是依赖 onError 里的单点清除。如果需要彻底重置,可取消注释:
// this.clearCache();
2025-12-29 11:53:19 +08:00
// 4. 强制重置加载锁
this.isLoading = false;
this.loadingPromise = null;
this.currentLoadingIndex = 0;
// 5. 重新初始化 (initAudios 会自增 loadGeneration从而终止之前的任何异步循环)
return this.initAudios();
}
2025-07-14 16:37:56 +08:00
}
// 导出单例
2025-11-27 18:16:32 +08:00
export default new AudioManager();