From 5ee8e159658eb25bd4e89edd61dc81aa8571b2f1 Mon Sep 17 00:00:00 2001 From: mapleafgo Date: Wed, 8 Apr 2026 11:34:27 +0800 Subject: [PATCH] =?UTF-8?q?fix(audio):=20=E4=BF=AE=E5=A4=8D=E9=9F=B3?= =?UTF-8?q?=E9=A2=91=E6=92=AD=E6=94=BE=E6=AD=BB=E5=BE=AA=E7=8E=AF=E5=B9=B6?= =?UTF-8?q?=E5=A2=9E=E5=BC=BA=E9=94=99=E8=AF=AF=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复 PlayWav 和 PlayMP3 在 context 取消时的死循环 bug - 添加 WAV/MP3 解码失败的错误日志 - 添加 TTS 播放开始/完成的日志,便于排查问题 修复前 context 取消会导致无限循环,阻塞后续任务执行。 Co-Authored-By: Claude Sonnet 4.6 --- pkg/audio/play.go | 4 ++++ pkg/tts/aliyun.go | 2 ++ 2 files changed, 6 insertions(+) diff --git a/pkg/audio/play.go b/pkg/audio/play.go index 62c3378..55af1b4 100644 --- a/pkg/audio/play.go +++ b/pkg/audio/play.go @@ -24,6 +24,7 @@ func init() { func PlayWav(c context.Context, r io.Reader) { streamer, format, err := wav.Decode(r) if err != nil { + zap.S().Errorln("WAV解码失败: ", err) return } defer streamer.Close() @@ -45,6 +46,7 @@ func PlayWav(c context.Context, r io.Reader) { speaker.Lock() ctrl.Streamer = nil speaker.Unlock() + return } } } @@ -53,6 +55,7 @@ func PlayWav(c context.Context, r io.Reader) { func PlayMP3(c context.Context, r io.ReadCloser) { streamer, format, err := mp3.Decode(r) if err != nil { + zap.S().Errorln("MP3解码失败: ", err) return } defer streamer.Close() @@ -74,6 +77,7 @@ func PlayMP3(c context.Context, r io.ReadCloser) { speaker.Lock() ctrl.Streamer = nil speaker.Unlock() + return } } } diff --git a/pkg/tts/aliyun.go b/pkg/tts/aliyun.go index 4e9ae61..a476d21 100644 --- a/pkg/tts/aliyun.go +++ b/pkg/tts/aliyun.go @@ -45,9 +45,11 @@ func (tts *AliTTS) Sound(text string) { if text == "" { return } + zap.S().Infof("开始播放TTS: %s", text) buf, err := tts.Get(text) if err == nil && buf != nil { audio.PlayWav(tts.ctx, buf) + zap.S().Infof("TTS播放完成: %s", text) } else { zap.S().Errorln("AliTTS 请求异常: ", err) }