fix(audio): 修复音频播放死循环并增强错误日志
All checks were successful
ci/woodpecker/tag/woodpecker Pipeline was successful
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>
This commit is contained in:
@@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user