Multiple audio instances not playing simultaneously with version 6.1.x #1883
-
Hello, I recently upgraded Flutter to the latest version (3.27.4). I'm not sure if it's related, but since then, my multiple audio instances no longer play simultaneously. I have this really simple code. When I press the first button ("Test1"), sound.mp3 starts playing. However, when I press the second button ("Test2"), it stops the first sound and plays only the second one. Before the upgrade, it worked as expected: the second sound played over the first one simultaneously. Could I have been using it incorrectly all this time? Or is this a known issue? Can anyone confirm if they are experiencing the same behavior? If so, should I just wait for a fix? Here is my code: class Page extends StatefulWidget {
const Page({super.key});
@override
PageState createState() => PageState();
}
class PageState extends State<Page> {
final AudioPlayer _player1 = AudioPlayer();
final AudioPlayer _player2 = AudioPlayer();
@override
void initState() {
super.initState();
_player1.setReleaseMode(ReleaseMode.release);
_player1.setVolume(1.0);
_player2.setReleaseMode(ReleaseMode.release);
_player2.setVolume(1.0);
}
@override
void dispose() {
_player1.dispose();
_player2.dispose();
super.dispose();
}
void play1() async {
await _player1.play(AssetSource('sounds/sound.mp3'));
}
void play2() async {
await _player2.play(AssetSource('sounds/pool/sound.wav'));
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: play1,
child: const Text('Test1'),
),
ElevatedButton(
onPressed: play2,
child: const Text('Test2'),
),
],
),
);
}
}
Thanks for your help! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Actually we already changed this behavior with 6.0.0 already, so I thought this is also enforced in Android, which was not the case. We fixed this with version 6.1.2 (without knowing it wasn't actually working before), so no major version upgrade was done, unfortunately. So what you actually have to do is setting the audio context, before starting to play e.g. in the final audioContext = AudioContextConfig(
focus: AudioContextConfigFocus.mixWithOthers,
).build();
// Either this before creating a player:
await AudioPlayer.global.setAudioContext(audioContext);
// Or if the player was already created or only a specific player needs this `mixWithOthers` option:
await player.setAudioContext(audioContext); |
Beta Was this translation helpful? Give feedback.
Actually we already changed this behavior with 6.0.0 already, so I thought this is also enforced in Android, which was not the case. We fixed this with version 6.1.2 (without knowing it wasn't actually working before), so no major version upgrade was done, unfortunately.
https://pub.dev/packages/audioplayers/changelog#600
So what you actually have to do is setting the audio context, before starting to play e.g. in the
initState
method:https://github.com/bluefireteam/audioplayers/blob/main/getting_started.md#audio-context