Repository navigation
fix: Keep Bgm stopped when stop is called while play is loading #4256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ufrshubham
merged 3 commits into
flame-engine:main
from
matthewalberto:fix/bgm-stop-during-play
Nov 28, 2026
+145
−3
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| import 'dart:async'; | ||
|
|
||
| import 'package:audioplayers/audioplayers.dart'; | ||
| import 'package:flame_audio/bgm.dart'; | ||
| import 'package:flutter/services.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:mocktail/mocktail.dart'; | ||
|
|
||
| class _MockAudioPlayer() extends Mock implements AudioPlayer; | ||
|
|
||
| void main() { | ||
| TestWidgetsFlutterBinding.ensureInitialized(); | ||
|
|
||
| setUpAll(() { | ||
| registerFallbackValue(AssetSource('')); | ||
| registerFallbackValue(ReleaseMode.loop); | ||
| final messenger = | ||
| TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger; | ||
| for (final name in [ | ||
| 'xyz.luan/audioplayers', | ||
| 'xyz.luan/audioplayers.global', | ||
| ]) { | ||
| messenger.setMockMethodCallHandler( | ||
| MethodChannel(name), | ||
| (_) async => null, | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| group('Bgm', () { | ||
| late _MockAudioPlayer player; | ||
| late Bgm bgm; | ||
| late List<Completer<void>> pendingSources; | ||
|
|
||
| setUp(() { | ||
| player = _MockAudioPlayer(); | ||
| pendingSources = []; | ||
| when(() => player.release()).thenAnswer((_) async {}); | ||
| when(() => player.setReleaseMode(any())).thenAnswer((_) async {}); | ||
| when(() => player.setVolume(any())).thenAnswer((_) async {}); | ||
| when(() => player.setSource(any())).thenAnswer((_) { | ||
| final completer = Completer<void>(); | ||
| pendingSources.add(completer); | ||
| return completer.future; | ||
| }); | ||
| when(() => player.resume()).thenAnswer((_) async {}); | ||
| when(() => player.stop()).thenAnswer((_) async {}); | ||
| bgm = Bgm()..audioPlayer = player; | ||
| }); | ||
|
|
||
| Future<void> untilSourceIsLoading(int count) async { | ||
| while (pendingSources.length < count) { | ||
| await Future<void>.delayed(Duration.zero); | ||
| } | ||
| } | ||
|
|
||
| test('stop while play is still loading keeps the music stopped', () async { | ||
| final playing = bgm.play('song.mp3'); | ||
| await untilSourceIsLoading(1); | ||
|
|
||
| await bgm.stop(); | ||
| pendingSources.single.complete(); | ||
| await playing; | ||
|
|
||
| verifyNever(() => player.resume()); | ||
| expect(bgm.isPlaying, isFalse); | ||
| }); | ||
|
|
||
| test('play after stop starts the music', () async { | ||
| await bgm.stop(); | ||
| final playing = bgm.play('song.mp3'); | ||
| await untilSourceIsLoading(1); | ||
| pendingSources.single.complete(); | ||
| await playing; | ||
|
|
||
| verify(() => player.resume()).called(1); | ||
| expect(bgm.isPlaying, isTrue); | ||
| }); | ||
|
|
||
| test('stop while the music is starting leaves isPlaying false', () async { | ||
| final resuming = Completer<void>(); | ||
| when(() => player.resume()).thenAnswer((_) => resuming.future); | ||
| final playing = bgm.play('song.mp3'); | ||
| await untilSourceIsLoading(1); | ||
| pendingSources.single.complete(); | ||
| await Future<void>.delayed(Duration.zero); | ||
|
|
||
| await bgm.stop(); | ||
| resuming.complete(); | ||
| await playing; | ||
|
|
||
| verify(() => player.stop()).called(1); | ||
| expect(bgm.isPlaying, isFalse); | ||
| }); | ||
|
|
||
| test( | ||
| 'play while another play is loading starts only the last one', | ||
| () async { | ||
| final first = bgm.play('first.mp3'); | ||
| await untilSourceIsLoading(1); | ||
|
|
||
| final second = bgm.play('second.mp3'); | ||
| await untilSourceIsLoading(2); | ||
| pendingSources[1].complete(); | ||
| await second; | ||
| pendingSources[0].complete(); | ||
| await first; | ||
|
|
||
| verify(() => player.resume()).called(1); | ||
| expect(bgm.isPlaying, isTrue); | ||
| }, | ||
| ); | ||
|
|
||
| test('play whose resume fails leaves isPlaying false', () async { | ||
| when(() => player.resume()).thenThrow(Exception('resume failed')); | ||
| final playing = bgm.play('song.mp3'); | ||
| await untilSourceIsLoading(1); | ||
| pendingSources.single.complete(); | ||
|
|
||
| await expectLater(playing, throwsException); | ||
| expect(bgm.isPlaying, isFalse); | ||
| }); | ||
| }); | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, but this one predates this PR: before it the older play also resumed, so the wrong track played outright, and this PR only stops that extra resume. Serialising setup with a lock changes how overlapping play calls behave, so I would rather open it as a follow-up. Happy to add it here instead if you prefer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is better to fix this separately.