fix: 修复新旧 TTS 语音重叠播放的问题

- TTSPlayer 新增 stopCurrentAudio() 方法,play() 开始前先停止旧音频
- onSpeechEnd 发送新 query 前停止上一轮 TTS 播放
This commit is contained in:
2026-06-14 12:01:34 +08:00
parent eb7ebecfc0
commit 24a6ce5295
2 changed files with 20 additions and 0 deletions

View File

@@ -47,6 +47,19 @@ export class TTSPlayer {
this._isPlaying = false;
}
/**
* 停止当前正在播放的音频(不清空 chunks
* 用于 play() 开始前,防止新旧音频重叠
*/
private stopCurrentAudio(): void {
if (this.audio) {
this.audio.pause();
this.audio.removeAttribute("src");
this.audio = null;
}
this._isPlaying = false;
}
/** 暂停播放 */
pause(): void {
this.audio?.pause();
@@ -61,6 +74,9 @@ export class TTSPlayer {
private play(mimeType: string): void {
if (this.chunks.length === 0) return;
// 停止当前正在播放的音频,防止重叠
this.stopCurrentAudio();
// 拼接所有 Base64 片段
const combined = this.chunks.join("");
this.chunks = [];