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
This commit is contained in:
Hajime Hoshi 2024-11-06 21:42:43 +09:00
parent 20014ad5bc
commit 7b5054ca3a

View File

@ -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
}