Skip to content

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

Done

Description

@matthewalberto

What happened?

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

FlameAudio.bgm.play() loads its track into one shared AudioPlayer. If play() is called again before the first track has loaded, both loads run at the same time. When the older one finishes last, its source goes to the player after the newer track has already started, and replaces it. Since #4256 the older call no longer calls resume(), but its setSource() still lands.

What do you expect?

The track from the last play() is the one left playing, however long the earlier ones take to load.

How can we reproduce this?

// menu.mp3 takes longer to load than battle.mp3, for example because it is
// larger and not cached yet.
unawaited(FlameAudio.bgm.play('music/menu.mp3'));
// A moment later the game switches music.
await FlameAudio.bgm.play('music/battle.mp3');

With a real Bgm and AudioPlayer, a fake platform underneath and an AudioCache that delays the first file, the platform receives source short.mp3, resume, then source long.mp3.

What steps should take to fix this?

Let one play() at a time load into the player, and keep stop() outside that wait so it still takes effect at once. I have a fix with tests ready and will open a PR.

Affected platforms

All, as the order of calls is decided in Dart. On Android, audioplayers_android prepares and starts a source that is set while the player is playing (read in WrappedPlayer, not tried on a device).

Other information

Flame main at 10f9897 (flame_audio 2.13.0-dev.1, audioplayers 6.8.1), Flutter 3.47.9 stable.

Are you interested in working on a PR for this?

  • I want to work on this

Activity

  1. matthewalberto commented on Nov 30, 2026

    @matthewalberto
    ContributorAuthor

    Fix in #4261.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions