1

Actually, I want to play all the sounds in raw folder in sequence. When I click the button only the first one is played. I will be grateful for your help . This is my code:

public class MainActivity extends AppCompatActivity {
private SoundPool soundPool;
private int sound1, sound2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .build();

        soundPool = new SoundPool.Builder()
                .setMaxStreams(2)
                .setAudioAttributes(audioAttributes)
                .build();
    } else {
        soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
    }

    sound1 = soundPool.load(this, R.raw.a, 1);
    sound2 = soundPool.load(this, R.raw.d, 1);

}
public void playSound(View v) {
    soundPool.play(sound1, 1, 1, 0, 0, 1);
    soundPool.play(sound2, 1, 1, 0, 0, 1);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    soundPool.release();
    soundPool = null;
}
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Dler
  • 21
  • 1
  • 3
  • Possible duplicate of [How to play a sound simultaneously using SoundPool for a specific period of time](https://stackoverflow.com/questions/17509608/how-to-play-a-sound-simultaneously-using-soundpool-for-a-specific-period-of-time) – emandt Mar 07 '19 at 12:51
  • @emandt sequence are simultaneous have different meanings. So it's not a duplicate of it – Vivek Mishra Mar 07 '19 at 12:54
  • I want the second sound will be played after the first one finished – Dler Mar 07 '19 at 13:06
  • How about declaring a new int variable, " int allSounds==sound1 + sound2;" and then " soundPool.play(allSounds, 1, 1, 0, 0, 1);"? – SafalFrom2050 Mar 07 '19 at 15:02

1 Answers1

0

I used Howard Liu's SoundPoolPlayer class, a wrapper around the SoundPool that calculates the duration of the audio file playing (using the MediaPlayer) and calls the custom OnCompletionListener event after the playback is finished using postDelayed.

In the handling of the OnCompletionListener event, you just need to start playing the next audio file.