From 5121b072f180151a4e118d2f73c834274bfff25e Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Thu, 2 May 2019 02:23:41 +0900 Subject: [PATCH] audio: Change init from a variable to a channel --- audio/audio.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/audio/audio.go b/audio/audio.go index 259c85451..1c1fe0a20 100644 --- a/audio/audio.go +++ b/audio/audio.go @@ -55,9 +55,13 @@ const ( type Context struct { c context + // inited represents whether the audio device is initialized and available or not. + // On Android, audio loop cannot be started unless JVM is accessible. After updating one frame, JVM should exist. + inited chan struct{} + initedOnce sync.Once + sampleRate int err error - inited bool suspended bool ready bool @@ -94,6 +98,7 @@ func NewContext(sampleRate int) (*Context, error) { sampleRate: sampleRate, c: newContext(sampleRate), players: map[*playerImpl]struct{}{}, + inited: make(chan struct{}), } theContext = c @@ -110,10 +115,9 @@ func NewContext(sampleRate int) (*Context, error) { }) h.AppendHookOnBeforeUpdate(func() error { - // On Android, audio loop cannot be started unless JVM is accessible. After updating one frame, JVM should exist. - c.m.Lock() - c.inited = true - c.m.Unlock() + c.initedOnce.Do(func() { + close(c.inited) + }) var err error theContextLock.Lock() @@ -139,10 +143,9 @@ func CurrentContext() *Context { func (c *Context) playable() bool { c.m.Lock() - i := c.inited s := c.suspended c.m.Unlock() - return i && !s + return !s } func (c *Context) hasError() bool { @@ -393,6 +396,8 @@ func (p *playerImpl) Play() { } func (p *playerImpl) loop() { + <-p.context.inited + w := p.context.c.NewPlayer() wclosed := make(chan struct{}) defer func() {