待机功能基本实现

This commit is contained in:
2024-11-06 15:44:35 +08:00
parent 8e2bf7f59b
commit ab0678aa3b
14 changed files with 317 additions and 151 deletions

View File

@@ -1,19 +1,51 @@
package middleware
import (
"game-driver/internal/common"
"game-driver/internal/schema"
"game-driver/leaf"
"game-driver/pkg/audio"
"github.com/gopxl/beep/v2/speaker"
"log"
"sync"
)
func PlayBgm() leaf.HandlerFunc {
return func(c *leaf.Context) {
pm := leaf.Value[*schema.PlayModal](c, PayloadJSONKey)
bgm := common.LinkAudio(pm.BGM)
bgm := audio.LinkAudio(pm.BGM)
if bgm != nil {
go audio.PlayMP3(c, bgm)
// 等待组
var wait sync.WaitGroup
defer wait.Wait()
// 结束信号通道
a := make(chan struct{})
// 发送结束信号
defer close(a)
go func() {
// 等待结束
wait.Add(1)
defer wait.Done()
ctrl, closer := audio.PlayBgmMP3(bgm)
defer closer()
if ctrl == nil {
log.Println("播放背景音乐失败")
return
}
select {
case <-a:
{
speaker.Lock()
ctrl.Streamer = nil
speaker.Unlock()
return
}
}
}()
}
c.Next()

View File

@@ -7,17 +7,17 @@ import (
)
// SoundStart 开始词播报
func SoundStart(t *tts.AliTTS) leaf.HandlerFunc {
func SoundStart() leaf.HandlerFunc {
return func(c *leaf.Context) {
pm := leaf.Value[*schema.PlayModal](c, PayloadJSONKey)
t.Sound(pm.TTS.Start)
tts.DefaultTTS.Sound(pm.TTS.Start)
defer func() {
switch leaf.Value[leaf.EndType](c, leaf.EndKey) {
case leaf.EndTimer:
t.Sound(pm.TTS.End)
tts.DefaultTTS.Sound(pm.TTS.End)
case leaf.EndStop:
t.Sound(pm.TTS.Stop)
tts.DefaultTTS.Sound(pm.TTS.Stop)
}
}()

View File

@@ -8,7 +8,7 @@ import (
"time"
)
func TickerAction(t *tts.AliTTS) leaf.HandlerFunc {
func TickerAction() leaf.HandlerFunc {
return func(c *leaf.Context) {
pm := leaf.Value[*schema.PlayModal](c, PayloadJSONKey)
@@ -50,7 +50,7 @@ func TickerAction(t *tts.AliTTS) leaf.HandlerFunc {
//TODO: 屏幕打印
}
if to, ok := ttsMap[s]; ok {
t.Sound(to.Value)
tts.DefaultTTS.Sound(to.Value)
}
}
}