This commit is contained in:
Daniel 2024-08-04 23:50:14 -04:00
parent 4211a9cf8c
commit ff76892a70

View file

@ -9,7 +9,9 @@ import (
) )
type Player struct { type Player struct {
ctx *oto.Context ctx *oto.Context
sampleRate int
channels int
} }
func NewPlayer() *Player { func NewPlayer() *Player {
@ -19,13 +21,18 @@ func NewPlayer() *Player {
} }
func (p *Player) initOto(samp, channels int) error { func (p *Player) initOto(samp, channels int) error {
if p.ctx != nil { if samp != p.sampleRate || channels != p.channels {
return nil if p.ctx != nil {
} err := p.ctx.Close()
if err != nil {
return err
}
}
var err error var err error
if p.ctx, err = oto.NewContext(samp, channels, 2, 1024); err != nil { if p.ctx, err = oto.NewContext(samp, channels, 2, 1024); err != nil {
return err return err
}
} }
return nil return nil
@ -62,6 +69,7 @@ func (p *Player) Play(audio []byte, mimeType string) error {
case "audio/mpeg": case "audio/mpeg":
return p.playMP3(audio) return p.playMP3(audio)
case "audio/wav": case "audio/wav":
panic("wav not implemented yet")
default: default:
return fmt.Errorf("unknown format %s", mimeType) return fmt.Errorf("unknown format %s", mimeType)
} }