0

I'm trying to play two sound tracks at the time, the first one work as a BGM and the other works as a SFX

What happened now is the BGM is playing and when a SFX sound start to play the BGM is stopped and then starts from the beginning.

I want both sounds to be played without stopping, I searched the docs and I couldn't found a clear answer to this.

Ali Habbash
  • 777
  • 2
  • 6
  • 29
  • Maybe a duplicate of this? https://stackoverflow.com/questions/65390691/how-to-play-two-or-more-audio-files-simultaneously-with-just-audio-and-audio-s – Ryan Heise Apr 13 '21 at 16:06

2 Answers2

0

You can use asset_audio_player to open multiple audio instances.

///play 3 songs in parallel
AssetsAudioPlayer.newPlayer().open(
    Audio.asset("assets/audios/song1.mp3")
);
AssetsAudioPlayer.newPlayer().open(
    Audio.asset("assets/audios/song2.mp3")
);

//another way, with create, open, play & dispose the player on finish
AssetsAudioPlayer.playAndForget(
    Audio.asset("assets/audios/song3.mp3")
);

You can read the documentation fore more info.

Mayb3Not
  • 441
  • 4
  • 10
0

You can refer to this question for a more comprehensive answer, but you can simply create two players, one to play the BGM and one to play the SFX:

final bgmPlayer = AudioPlayer();
final sfxPlayer = AudioPlayer();

await bgmPlayer.setAsset('music.mp3');
await sfxPlayer.setAsset('effect.mp3');

bgmPlayer.play(); // start playing background music

// now you can play the sound effect any time you want
// while the BGM continues to play:
await sfxPlayer.play();
await sfxPlayer.play();
await sfxPlayer.play();
Ryan Heise
  • 2,273
  • 5
  • 14