How to play two sounds at the same time? How to play two sounds at the same time? dart dart

How to play two sounds at the same time?


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.


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!