audio: Bug fix: (*Context).IsReady never returned true unless there is a player

The audio context is never ready unless there is a player. This is
problematic when a user tries to play audio after the context is
ready. Play a dummy player to avoid the blocking.

Fixes #969
This commit is contained in:
Hajime Hoshi 2019-10-22 23:45:00 +09:00
parent f34e94ff2c
commit b78f678700

View File

@ -185,8 +185,25 @@ func (c *Context) removePlayer(p *playerImpl) {
// On some browsers, user interaction like click or pressing keys is required to start audio.
func (c *Context) IsReady() bool {
c.m.Lock()
defer c.m.Unlock()
r := c.ready
c.m.Unlock()
if r {
return r
}
if len(c.players) != 0 {
return r
}
// Create another goroutine since (*Player).Play can lock the context's mutex.
go func() {
// The audio context is never ready unless there is a player. This is
// problematic when a user tries to play audio after the context is ready.
// Play a dummy player to avoid the blocking (#969).
p, _ := NewPlayerFromBytes(c, make([]byte, bytesPerSample))
p.Play()
}()
return r
}