Skip to content

Commit

Permalink
Add speaker.Suspend and speaker.Resume
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkKremer committed Jan 24, 2024
1 parent f2cc5a2 commit 31188f2
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion speaker/speaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func Init(sampleRate beep.SampleRate, bufferSize int) error {

var err error
var readyChan chan struct{}
context, readyChan, err := oto.NewContext(&oto.NewContextOptions{
context, readyChan, err = oto.NewContext(&oto.NewContextOptions{
SampleRate: int(sampleRate),
ChannelCount: channelCount,
Format: otoFormat,
Expand Down Expand Up @@ -95,6 +95,27 @@ func Play(s ...beep.Streamer) {
mu.Unlock()
}

// Suspend suspends the entire audio play.
//
// This function is intended to save resources when no audio is playing.
// To suspend individual streams, use the beep.Ctrl.
func Suspend() error {
err := context.Suspend()
if err != nil {
return errors.Wrap(err, "failed to suspend the speaker")
}
return nil
}

// Resume resumes the entire audio play, which was suspended by Suspend.
func Resume() error {
err := context.Resume()
if err != nil {
return errors.Wrap(err, "failed to resume the speaker")
}
return nil
}

// Clear removes all currently playing Streamers from the speaker.
// Previously buffered samples may still be played.
func Clear() {
Expand Down

0 comments on commit 31188f2

Please sign in to comment.