Skip to content

fix: Keep a late-loading track from replacing a newer one in Bgm - #4261

Merged
spydon merged 1 commit into
flame-engine:mainfrom
matthewalberto:fix/bgm-newest-play-wins
Dec 1, 2026
Merged

spydon merged 1 commit into
flame-engine:mainfrom
matthewalberto:fix/bgm-newest-play-wins

Conversation

@matthewalberto

Copy link
Copy Markdown
Contributor

Description

Follow-up to #4256, where this was left to be fixed separately.

Bgm.play() loads into one shared AudioPlayer. Two overlapping calls loaded at the same time, so when the older track finished loading last, its setSource reached 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 (synchronized is already a dependency of flame_audio). A play() 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 waiting play() 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 audioplayers times 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.dart are new or changed. The overlap test now checks which source the player ends up holding, and the others cover a replaced waiting play, stop() and dispose() while a play waits, and a failed load not blocking the next one. Four of them fail on main. 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 whole flame_audio suite passes (10 tests), and dart format, flutter analyze and cspell are clean. I also ran a throwaway test with a real AudioPlayer and a fake platform: on main the 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

  • I have followed the Contributor Guide when preparing my PR.
  • I have updated/added tests for ALL new/updated/fixed functionality.
  • I have updated/added relevant documentation in docs and added dartdoc comments with ///.
  • [-] I have updated/added relevant examples in examples or docs.

Breaking Change?

  • Yes, this PR is a breaking change.
  • No, this PR is not a breaking change.

Related Issues

Closes #4260

@spydon spydon left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm, thanks for your contribution

@spydon
spydon added this pull request to the merge queue Dec 1, 2026
Merged via the queue into flame-engine:main with commit 2b9458c Dec 1, 2026
9 checks passed
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[flame_audio] An older Bgm track that finishes loading late replaces the newer one

2 participants