Repository navigation
fix: Keep a late-loading track from replacing a newer one in Bgm - #4261
Merged
Merged
Conversation
1 task done
matthewalberto
added a commit
to matthewalberto/matthewalberto
that referenced
this pull request
Nov 30, 2026
spydon
approved these changes
Dec 1, 2026
spydon
left a comment
Member
There was a problem hiding this comment.
Lgtm, thanks for your contribution
matthewalberto
added a commit
to matthewalberto/matthewalberto
that referenced
this pull request
Dec 2, 2026
spydon
pushed a commit
that referenced
this pull request
Dec 4, 2026
…oading (#4307) Follow-up to #4256 and #4261, for `pause()` and the lifecycle handler. When `pause()` was called, or the app left the foreground, while `Bgm.play()` was still loading its track, `play()` still called `resume()` once the track had loaded, so the music started after the game paused it, or played in the background. Now `pause()` sets a flag that `play()` checks after loading: the track stays loaded and `isPlaying` stays false, and `resume()` starts it. `play()` and `resume()` clear the flag. `didChangeAppLifecycleState` also records whether the app is in the resumed state. A track that finishes loading while it is not sets `isPlaying` but is not started, and the `resumed` handler starts it. For that, the `resumed` handler now restarts a player in `PlayerState.stopped` as well as `paused`, when `isPlaying` is true. In `audioplayers` 6.8.1 the state is set in Dart: `release()` calls `stop()`, which sets `stopped`, and `setSource()` leaves the state alone. So a loaded track that was never started is `stopped` if the app left the foreground before `play()` released the player, and `paused` if it left after. Testing: 8 new tests in `test/bgm_test.dart`. 4 fail on `main` (pause while loading, resume after that pause, and the two background cases). The other 4 guard the new flags (play after a pause, pause and resume both during the load, the app coming back before the load ends, and the app coming back after a `pause()`). I broke each part of the fix on its own (10 changes) and a test failed each time. The whole `flame_audio` suite passes (18 tests), and `dart format`, `flutter analyze` and cspell are clean. I also ran a throwaway test with a real `Bgm` and `AudioPlayer` and a fake platform: on `main` the platform gets `resume` after the pause in all three cases, and with this change it gets `resume` only after `resume()` or the app coming back. Not tested: on a device or in a running game.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Follow-up to #4256, where this was left to be fixed separately.
Bgm.play()loads into one sharedAudioPlayer. Two overlapping calls loaded at the same time, so when the older track finished loading last, itssetSourcereached the player after the newer track had started, and replaced it.Now a lock lets one
play()at a time release the player and load its source (synchronizedis already a dependency offlame_audio). Aplay()that was replaced or stopped while it waited for the lock loads nothing.stop()does not take the lock, so it still takes effect at once.dispose()now also moves the play counter on, so a waitingplay()does not touch a disposed player.The cost: a new track starts to load only after the one before it has finished loading. If that earlier load hangs, the new track waits until
audioplayerstimes it out (AudioPlayer.preparationTimeout). The other way I saw was to keep loading both at once and load the newest source again after a late one lands, which avoids the wait but restarts the newest track.Testing: five tests in
test/bgm_test.dartare new or changed. The overlap test now checks which source the player ends up holding, and the others cover a replaced waiting play,stop()anddispose()while a play waits, and a failed load not blocking the next one. Four of them fail onmain. The failed-load one passes there, as it guards the new lock. I broke each part of the fix on its own and a test failed each time. The wholeflame_audiosuite passes (10 tests), anddart format,flutter analyzeand cspell are clean. I also ran a throwaway test with a realAudioPlayerand a fake platform: onmainthe last source the platform receives is the old track, and with this change it is the new one. Not tested: on a device or in a running game, and on Linux or macOS.Checklist
docsand added dartdoc comments with///.examplesordocs.Breaking Change?
Related Issues
Closes #4260