加载策略调整
This commit is contained in:
@@ -300,17 +300,6 @@ class AudioManager {
|
|||||||
|
|
||||||
// 播放指定音频或音频数组(数组则按顺序连续播放)
|
// 播放指定音频或音频数组(数组则按顺序连续播放)
|
||||||
play(input) {
|
play(input) {
|
||||||
// 离线:缓存播放意图(可为字符串或数组),待网络恢复后自动播放
|
|
||||||
if (!this.networkOnline) {
|
|
||||||
this.pendingPlayKey = input;
|
|
||||||
debugLog(
|
|
||||||
`网络不可用,记录播放意图: ${
|
|
||||||
Array.isArray(input) ? input.join(",") : input
|
|
||||||
}`
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 再次调用 play:打断前面所有声音与队列
|
// 再次调用 play:打断前面所有声音与队列
|
||||||
this.stopAll();
|
this.stopAll();
|
||||||
this.isSequenceRunning = false;
|
this.isSequenceRunning = false;
|
||||||
@@ -339,10 +328,15 @@ class AudioManager {
|
|||||||
// 内部方法:播放单个 key
|
// 内部方法:播放单个 key
|
||||||
_playSingle(key, forceStopAll = false) {
|
_playSingle(key, forceStopAll = false) {
|
||||||
if (!this.networkOnline) {
|
if (!this.networkOnline) {
|
||||||
|
const audio = this.audioMap.get(key);
|
||||||
|
const isReady = this.readyMap && this.readyMap.get(key);
|
||||||
|
// 离线但已就绪:允许直接播放;未就绪则记录等待重连
|
||||||
|
if (!audio || !isReady) {
|
||||||
this.pendingPlayKey = key;
|
this.pendingPlayKey = key;
|
||||||
debugLog(`网络不可用,记录播放意图: ${key}`);
|
debugLog(`网络不可用,记录播放: ${key}`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (forceStopAll) {
|
if (forceStopAll) {
|
||||||
this.stopAll();
|
this.stopAll();
|
||||||
@@ -451,17 +445,34 @@ class AudioManager {
|
|||||||
} catch (_) {}
|
} catch (_) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 重连回调:重建全部音频后,重放上一次的播放意图
|
// 重连回调:重建全部音频后,重放上一次的播放
|
||||||
onNetworkRestored() {
|
onNetworkRestored() {
|
||||||
this.reinitializeOnReconnect();
|
// 网络恢复后,仅继续加载未就绪或缺失的音频,已就绪的不动,保证离线可播的缓存不被重置
|
||||||
|
const keys = Object.keys(audioFils);
|
||||||
|
const needLoad = keys.filter(
|
||||||
|
(k) => !this.readyMap.get(k) || !this.audioMap.has(k)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (needLoad.length > 0) {
|
||||||
|
this.loadKeysSequentially(needLoad, () => {
|
||||||
if (this.pendingPlayKey) {
|
if (this.pendingPlayKey) {
|
||||||
const key = this.pendingPlayKey;
|
const pending = this.pendingPlayKey;
|
||||||
this.pendingPlayKey = null;
|
this.pendingPlayKey = null;
|
||||||
// 避免与初始化竞态,轻微延迟后播放
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
this.play(key);
|
this.play(pending);
|
||||||
}, 20);
|
}, 20);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// 没有需要加载的音频,直接处理待播放
|
||||||
|
if (this.pendingPlayKey) {
|
||||||
|
const pending = this.pendingPlayKey;
|
||||||
|
this.pendingPlayKey = null;
|
||||||
|
setTimeout(() => {
|
||||||
|
this.play(pending);
|
||||||
|
}, 20);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 重连后统一重建音频实例
|
// 重连后统一重建音频实例
|
||||||
@@ -470,6 +481,29 @@ class AudioManager {
|
|||||||
this.initAudios();
|
this.initAudios();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 按自定义列表串行加载音频(避免并发过多)
|
||||||
|
loadKeysSequentially(keys, onComplete) {
|
||||||
|
let idx = 0;
|
||||||
|
const next = () => {
|
||||||
|
if (idx >= keys.length) {
|
||||||
|
if (onComplete) onComplete();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const k = keys[idx++];
|
||||||
|
// 已存在但未就绪:重载;不存在:创建
|
||||||
|
if (this.audioMap.has(k)) {
|
||||||
|
this.retryLoadAudio(k);
|
||||||
|
} else {
|
||||||
|
this.createAudio(k, () => {
|
||||||
|
setTimeout(next, 100);
|
||||||
|
});
|
||||||
|
return; // createAudio 内部会触发 next
|
||||||
|
}
|
||||||
|
setTimeout(next, 100);
|
||||||
|
};
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
|
||||||
// 手动重新加载指定音频
|
// 手动重新加载指定音频
|
||||||
reloadAudio(key) {
|
reloadAudio(key) {
|
||||||
if (audioFils[key]) {
|
if (audioFils[key]) {
|
||||||
|
|||||||
Reference in New Issue
Block a user