audio: Bug fix: Do not create players every call of IsReady

Close #1709
This commit is contained in:
Hajime Hoshi 2021-07-10 19:21:27 +09:00
parent 6d0ffeb7d8
commit 4b7064ac58

View File

@ -71,6 +71,7 @@ type Context struct {
sampleRate int sampleRate int
err error err error
ready bool ready bool
readyOnce sync.Once
players map[playerImpl]struct{} players map[playerImpl]struct{}
@ -255,15 +256,18 @@ func (c *Context) IsReady() bool {
return r return r
} }
// Create another goroutine since (*Player).Play can lock the context's mutex. c.readyOnce.Do(func() {
go func() { // Create another goroutine since (*Player).Play can lock the context's mutex.
// The audio context is never ready unless there is a player. This is // TODO: Is this needed for reader players?
// problematic when a user tries to play audio after the context is ready. go func() {
// Play a dummy player to avoid the blocking (#969). // The audio context is never ready unless there is a player. This is
// Use a long enough buffer so that writing doesn't finish immediately (#970). // problematic when a user tries to play audio after the context is ready.
p := NewPlayerFromBytes(c, make([]byte, bufferSize()*2)) // Play a dummy player to avoid the blocking (#969).
p.Play() // Use a long enough buffer so that writing doesn't finish immediately (#970).
}() p := NewPlayerFromBytes(c, make([]byte, bufferSize()*2))
p.Play()
}()
})
return r return r
} }