update:修复web测试工具Maximum call stack size exceeded 的bug

This commit is contained in:
hrz
2025-05-28 11:23:39 +08:00
parent 6a4ed78812
commit 7f864eb84d
+41 -33
View File
@@ -803,7 +803,10 @@
if (frameData && frameData.length > 0) { if (frameData && frameData.length > 0) {
// 转换为Float32 // 转换为Float32
const floatData = convertInt16ToFloat32(frameData); const floatData = convertInt16ToFloat32(frameData);
decodedSamples.push(...floatData); // 使用循环替代展开运算符
for (let i = 0; i < floatData.length; i++) {
decodedSamples.push(floatData[i]);
}
} }
} catch (error) { } catch (error) {
log("Opus解码失败: " + error.message, 'error'); log("Opus解码失败: " + error.message, 'error');
@@ -811,8 +814,10 @@
} }
if (decodedSamples.length > 0) { if (decodedSamples.length > 0) {
// 添加到解码队列 // 使用循环替代展开运算符
this.queue.push(...decodedSamples); for (let i = 0; i < decodedSamples.length; i++) {
this.queue.push(decodedSamples[i]);
}
this.totalSamples += decodedSamples.length; this.totalSamples += decodedSamples.length;
// 如果累积了至少0.2秒的音频,开始播放 // 如果累积了至少0.2秒的音频,开始播放
@@ -868,36 +873,39 @@
this.source = null; this.source = null;
this.playing = false; this.playing = false;
// 如果队列中还有数据或者缓冲区有新数据,继续播放 // 使用setTimeout避免递归调用
if (this.queue.length > 0) { setTimeout(() => {
setTimeout(() => this.startPlaying(), 10); // 如果队列中还有数据,继续播放
} else if (audioBufferQueue.length > 0) { if (this.queue.length > 0) {
// 缓冲区有新数据,进行解码 this.startPlaying();
const frames = [...audioBufferQueue]; } else if (audioBufferQueue.length > 0) {
audioBufferQueue = []; // 缓冲区有新数据,进行解码
this.decodeOpusFrames(frames); const frames = [...audioBufferQueue];
} else if (this.endOfStream) { audioBufferQueue = [];
// 流已结束且没有更多数据 this.decodeOpusFrames(frames);
log("音频播放完成", 'info'); } else if (this.endOfStream) {
isAudioPlaying = false; // 流已结束且没有更多数据
this.endOfStream = false; log("音频播放完成", 'info');
streamingContext = null; isAudioPlaying = false;
} else { this.endOfStream = false;
// 等待更多数据 streamingContext = null;
setTimeout(() => { } else {
// 如果仍然没有新数据,但有更多的包到达 // 等待更多数据
if (this.queue.length === 0 && audioBufferQueue.length > 0) { setTimeout(() => {
const frames = [...audioBufferQueue]; // 如果仍然没有新数据,但有更多的包到达
audioBufferQueue = []; if (this.queue.length === 0 && audioBufferQueue.length > 0) {
this.decodeOpusFrames(frames); const frames = [...audioBufferQueue];
} else if (this.queue.length === 0 && audioBufferQueue.length === 0) { audioBufferQueue = [];
// 真的没有更多数据了 this.decodeOpusFrames(frames);
log("音频播放完成 (超时)", 'info'); } else if (this.queue.length === 0 && audioBufferQueue.length === 0) {
isAudioPlaying = false; // 真的没有更多数据了
streamingContext = null; log("音频播放完成 (超时)", 'info');
} isAudioPlaying = false;
}, 500); // 500ms超时 streamingContext = null;
} }
}, 500); // 500ms超时
}
}, 10); // 10ms延迟,避免立即递归
}; };
this.source.start(); this.source.start();