1 Commits

Author SHA1 Message Date
5ee8e15965 fix(audio): 修复音频播放死循环并增强错误日志
All checks were successful
ci/woodpecker/tag/woodpecker Pipeline was successful
- 修复 PlayWav 和 PlayMP3 在 context 取消时的死循环 bug
- 添加 WAV/MP3 解码失败的错误日志
- 添加 TTS 播放开始/完成的日志,便于排查问题

修复前 context 取消会导致无限循环,阻塞后续任务执行。

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-08 11:34:27 +08:00
2 changed files with 6 additions and 0 deletions

View File

@@ -24,6 +24,7 @@ func init() {
func PlayWav(c context.Context, r io.Reader) { func PlayWav(c context.Context, r io.Reader) {
streamer, format, err := wav.Decode(r) streamer, format, err := wav.Decode(r)
if err != nil { if err != nil {
zap.S().Errorln("WAV解码失败: ", err)
return return
} }
defer streamer.Close() defer streamer.Close()
@@ -45,6 +46,7 @@ func PlayWav(c context.Context, r io.Reader) {
speaker.Lock() speaker.Lock()
ctrl.Streamer = nil ctrl.Streamer = nil
speaker.Unlock() speaker.Unlock()
return
} }
} }
} }
@@ -53,6 +55,7 @@ func PlayWav(c context.Context, r io.Reader) {
func PlayMP3(c context.Context, r io.ReadCloser) { func PlayMP3(c context.Context, r io.ReadCloser) {
streamer, format, err := mp3.Decode(r) streamer, format, err := mp3.Decode(r)
if err != nil { if err != nil {
zap.S().Errorln("MP3解码失败: ", err)
return return
} }
defer streamer.Close() defer streamer.Close()
@@ -74,6 +77,7 @@ func PlayMP3(c context.Context, r io.ReadCloser) {
speaker.Lock() speaker.Lock()
ctrl.Streamer = nil ctrl.Streamer = nil
speaker.Unlock() speaker.Unlock()
return
} }
} }
} }

View File

@@ -45,9 +45,11 @@ func (tts *AliTTS) Sound(text string) {
if text == "" { if text == "" {
return return
} }
zap.S().Infof("开始播放TTS: %s", text)
buf, err := tts.Get(text) buf, err := tts.Get(text)
if err == nil && buf != nil { if err == nil && buf != nil {
audio.PlayWav(tts.ctx, buf) audio.PlayWav(tts.ctx, buf)
zap.S().Infof("TTS播放完成: %s", text)
} else { } else {
zap.S().Errorln("AliTTS 请求异常: ", err) zap.S().Errorln("AliTTS 请求异常: ", err)
} }