Skip to content

Commit

Permalink
Merge pull request faiface#58 from avivklas/close-method
Browse files Browse the repository at this point in the history
Close method
  • Loading branch information
faiface authored May 21, 2019
2 parents 56e7c3c + 7cb0546 commit 8fd2689
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions speaker/speaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ func Init(sampleRate beep.SampleRate, bufferSize int) error {
mu.Lock()
defer mu.Unlock()

if player != nil {
done <- struct{}{}
player.Close()
context.Close()
}
Close()

mixer = beep.Mixer{}

Expand Down Expand Up @@ -63,6 +59,22 @@ func Init(sampleRate beep.SampleRate, bufferSize int) error {
return nil
}

// Close closes the playback and the driver. In most cases, there is certainly no need to call Close
// even when the program doesn't play anymore, because in properly set systems, the default mixer
// handles multiple concurrent processes. It's only when the default device is not a virtual but hardware
// device, that you'll probably want to manually manage the device from your application.
func Close() {
if player != nil {
if done != nil {
done <- struct{}{}
done = nil
}
player.Close()
context.Close()
player = nil
}
}

// Lock locks the speaker. While locked, speaker won't pull new data from the playing Stramers. Lock
// if you want to modify any currently playing Streamers to avoid race conditions.
//
Expand Down

0 comments on commit 8fd2689

Please sign in to comment.