mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-27 01:23:55 +08:00
优化:设置播放最短是0.3秒
This commit is contained in:
@@ -331,8 +331,8 @@
|
|||||||
// 开始播放音频
|
// 开始播放音频
|
||||||
startPlaying: async function () {
|
startPlaying: async function () {
|
||||||
while (true) {
|
while (true) {
|
||||||
// 如果累积了至少0.2秒的音频,开始播放
|
// 如果累积了至少0.3秒的音频,开始播放
|
||||||
const minSamples = SAMPLE_RATE * MIN_AUDIO_DURATION;
|
const minSamples = SAMPLE_RATE * MIN_AUDIO_DURATION * 3;
|
||||||
if (!this.playing && this.queue.length < minSamples) {
|
if (!this.playing && this.queue.length < minSamples) {
|
||||||
await this.getQueue(minSamples)
|
await this.getQueue(minSamples)
|
||||||
}
|
}
|
||||||
@@ -373,45 +373,6 @@
|
|||||||
}
|
}
|
||||||
await this.getQueue(minSamples)
|
await this.getQueue(minSamples)
|
||||||
}
|
}
|
||||||
// // 播放结束后的处理
|
|
||||||
// this.source.onended = () => {
|
|
||||||
// this.source = null;
|
|
||||||
// this.playing = false;
|
|
||||||
//
|
|
||||||
// // 使用setTimeout避免递归调用
|
|
||||||
// setTimeout(() => {
|
|
||||||
// // 如果队列中还有数据,继续播放
|
|
||||||
// if (this.queue.length > 0) {
|
|
||||||
// this.startPlaying();
|
|
||||||
// } else if (audioBufferQueue.length > 0) {
|
|
||||||
// // 缓冲区有新数据,进行解码
|
|
||||||
// const frames = [...audioBufferQueue];
|
|
||||||
// audioBufferQueue = [];
|
|
||||||
// this.decodeOpusFrames(frames);
|
|
||||||
// } else if (this.endOfStream) {
|
|
||||||
// // 流已结束且没有更多数据
|
|
||||||
// log("音频播放完成", 'info');
|
|
||||||
// isAudioPlaying = false;
|
|
||||||
// this.endOfStream = false;
|
|
||||||
// streamingContext = null;
|
|
||||||
// } else {
|
|
||||||
// // 等待更多数据
|
|
||||||
// setTimeout(() => {
|
|
||||||
// // 如果仍然没有新数据,但有更多的包到达
|
|
||||||
// if (this.queue.length === 0 && audioBufferQueue.length > 0) {
|
|
||||||
// const frames = [...audioBufferQueue];
|
|
||||||
// audioBufferQueue = [];
|
|
||||||
// this.decodeOpusFrames(frames);
|
|
||||||
// } else if (this.queue.length === 0 && audioBufferQueue.length === 0) {
|
|
||||||
// // 真的没有更多数据了
|
|
||||||
// log("音频播放完成 (超时)", 'info');
|
|
||||||
// isAudioPlaying = false;
|
|
||||||
// streamingContext = null;
|
|
||||||
// }
|
|
||||||
// }, 500); // 500ms超时
|
|
||||||
// }
|
|
||||||
// }, 10); // 10ms延迟,避免立即递归
|
|
||||||
// };
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user