From 7b5054ca3afe97a8db6dc43f9ba5cfc803aa5b43 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Wed, 6 Nov 2024 21:42:43 +0900 Subject: [PATCH] audio: bug fix: (*Player).Position is not updated correctly Update might not be called or delayed when the window is in background and invisible on macOS. Let's use a distinct groutine to update the audio player states. Closes #3154 --- audio/audio.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/audio/audio.go b/audio/audio.go index 254735a94..14296850a 100644 --- a/audio/audio.go +++ b/audio/audio.go @@ -143,13 +143,22 @@ func NewContext(sampleRate int) *Context { c.setReady() }() } - - if err := c.updatePlayers(); err != nil { - return err - } return nil }) + // In the current Ebitengine implementation, update might not be called when the window is in background (#3154). + // In this case, an audio player position is not updated correctly with AppendHookOnBeforeUpdate. + // Use a distinct goroutine to update the player states. + go func() { + for { + if err := c.updatePlayers(); err != nil { + c.setError(err) + return + } + time.Sleep(time.Second / 100) + } + }() + return c }