Reputation: 277687
I've been trying a few things with sounds in flutter lately . Using audioPlayer plugin.
But I'm facing a wall. I have both a background music, and small sounds effects. The problem is that I can't play both at once. If I play the background music, then the sound effect won't play. And the opposite works too.
Any idea about how to solve that issue ?
Upvotes: 3
Views: 1875
Reputation: 666
Use media players. Use this code to set up the background music:
MediaPlayer backgroundMediaPlayer = MediaPlayer.create(getApplicationContext(), Uri.parse(PATH_TO_BACKGROUND_MUSIC));
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
Then replace PATH_TO_BACKGROUND_MUSIC with the background music path.
Then when you want your sound effect to happen, use this code:
MediaPlayer soundEffectMediaPlayer = MediaPlayer.create(getApplicationContext(), Uri.parse(PATH_TO_SOUND_EFFECT));
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
Then replace PATH_TO_SOUND_EFFECT with the background music path.
You can learn for about media players here.
Hope this helps!
Upvotes: 0
Reputation: 116838
This is an issue with the audioplayer plugin and there's an issue open for it already. The author has indicated that he's open to pull requests if you'd like to take a crack at implementing it.
Upvotes: 5