current state of player
This commit is contained in:
@@ -6,13 +6,16 @@ import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/gopxl/beep"
|
||||
"github.com/gopxl/beep/mp3"
|
||||
"github.com/gopxl/beep/speaker"
|
||||
)
|
||||
|
||||
type Player struct {
|
||||
ctx context.Context
|
||||
state PlayerState
|
||||
ctx context.Context
|
||||
state PlayerState
|
||||
streamer beep.Streamer
|
||||
format beep.Format
|
||||
}
|
||||
|
||||
type PlayerState int
|
||||
@@ -36,23 +39,34 @@ func (p *Player) ChangeState(desiredState PlayerState) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Player) Play() error {
|
||||
f, err := os.Open("test_data/music_library_test/03 anything.mp3")
|
||||
func (p *Player) Play(filePath string) error {
|
||||
f, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to open file %w", err)
|
||||
return fmt.Errorf("failed to open file %w", err)
|
||||
}
|
||||
|
||||
if p.streamer == nil || p.format == nil {
|
||||
|
||||
}
|
||||
|
||||
streamer, format, err := mp3.Decode(f)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to decode mp3 %w", err)
|
||||
return fmt.Errorf("failed to decode mp3 %w", err)
|
||||
}
|
||||
defer streamer.Close()
|
||||
|
||||
// initialize the speaker with the sample rate from the file
|
||||
err = speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to initialize speaker %w", err)
|
||||
return fmt.Errorf("failed to initialize speaker %w", err)
|
||||
}
|
||||
speaker.Play(streamer)
|
||||
select {}
|
||||
|
||||
// hangs until song finishes playing
|
||||
done := make(chan bool)
|
||||
speaker.Play(beep.Seq(streamer, beep.Callback(func() {
|
||||
done <- true
|
||||
})))
|
||||
|
||||
<-done
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user