feat(audio): 使用 oto/v3 重构音频播放系统,移除 beep/v2 依赖
核心变更: - 实现全局 oto.Context 单例管理(sync.Once) - 实现一次性播放:PlayWav/PlayMP3(支持 context 取消) - 实现 BGM 循环播放:PlayMP3Loop(atomic.Bool + WaitGroup) - 迁移所有业务层到新 API(TTS/BGM/待机音频) - 添加完整的单元测试(6/6 通过) 技术栈: - oto/v3 v3.3.2(低级音频播放) - hajimehoshi/go-mp3 v0.3.4(MP3 解码) - youpy/go-wav v0.3.2(WAV 解码) 移除依赖: - gopxl/beep/v2 及所有相关依赖 优化: - 流式播放,无需预先加载 - 并发安全,无竞态条件 - 资源管理清晰(defer cleanup) - Sleep 间隔优化(1ms → 10ms,降低 CPU 占用)
This commit is contained in:
76
pkg/audio/loop_test.go
Normal file
76
pkg/audio/loop_test.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package audio
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestPlayMP3LoopStop(t *testing.T) {
|
||||
testFile := "testdata/test.mp3"
|
||||
if _, err := os.Stat(testFile); os.IsNotExist(err) {
|
||||
t.Skip("测试文件不存在:", testFile)
|
||||
}
|
||||
|
||||
f, err := os.Open(testFile)
|
||||
if err != nil {
|
||||
t.Fatalf("打开测试文件失败: %v", err)
|
||||
}
|
||||
|
||||
player, cleanup, err := PlayMP3Loop(f)
|
||||
if err != nil {
|
||||
t.Fatalf("PlayMP3Loop 失败: %v", err)
|
||||
}
|
||||
if player == nil {
|
||||
t.Fatal("player 不应为 nil")
|
||||
}
|
||||
|
||||
// 等待一小段时间确保播放开始
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
// 调用清理函数
|
||||
if err := cleanup(); err != nil {
|
||||
t.Errorf("cleanup 失败: %v", err)
|
||||
}
|
||||
|
||||
// 验证文件已关闭
|
||||
if err := f.Close(); err == nil {
|
||||
t.Error("文件应该已经被 cleanup 关闭")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConcurrentPlay(t *testing.T) {
|
||||
testFile := "testdata/test.mp3"
|
||||
if _, err := os.Stat(testFile); os.IsNotExist(err) {
|
||||
t.Skip("测试文件不存在:", testFile)
|
||||
}
|
||||
|
||||
// 启动多个并发播放
|
||||
const numPlayers = 3
|
||||
players := make([]any, numPlayers)
|
||||
cleanups := make([]func() error, numPlayers)
|
||||
|
||||
for i := 0; i < numPlayers; i++ {
|
||||
f, err := os.Open(testFile)
|
||||
if err != nil {
|
||||
t.Fatalf("打开测试文件 %d 失败: %v", i, err)
|
||||
}
|
||||
|
||||
player, cleanup, err := PlayMP3Loop(f)
|
||||
if err != nil {
|
||||
t.Fatalf("PlayMP3Loop %d 失败: %v", i, err)
|
||||
}
|
||||
players[i] = player
|
||||
cleanups[i] = cleanup
|
||||
}
|
||||
|
||||
// 等待播放
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
|
||||
// 清理所有播放器
|
||||
for i, cleanup := range cleanups {
|
||||
if err := cleanup(); err != nil {
|
||||
t.Errorf("cleanup %d 失败: %v", i, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user