There is a list of sound files:

static int[] sounds = {R.raw.cat1, R.raw.cat2, R.raw.cat3}; 

There is a file playback method :

 private void meowPlay(int fileName) { mp = MediaPlayer.create(this, fileName); mp.setLooping(false); mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { public void onPrepared(MediaPlayer mp) { mp.start(); } }); mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { public void onCompletion(MediaPlayer mp) { mp.release(); } }); } 

By pressing the button , the following code is executed:

 for (int i = 1; i <= 3; i++) { int rand = new Random().nextInt(2); meowPlay(sounds[rand]); } 

As a result, 3 audio files are played simultaneously . Can you please tell me how to make a sequential playback of these files?

    2 answers 2

    In fact, you do not play 3 files at the same time. You randomly input a number, and if the numbers match, the same sound resources will overlap on themselves and it may seem that either 1 file or 2 or 3 file is played and depends on the processor, it may seem like an echo effect depending on the speed cycle through. In general, the question is very interesting, here only through the listener of MediaPlayer, only "int resId" accepts input.

    Variables:

     MediaPlayer mp; int[] sounds; int index; 

    List:

     sounds = new int[]{R.raw.cat, R.raw.supercat, R.raw.megacat}; 

    Button handler:

      public void onClick (View v){ index = 0; mp = MediaPlayer.create(getApplicationContext(), sounds[index] ); mp.setLooping(false); mp.start(); mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){ public void onCompletion(MediaPlayer mp){ if(index < sounds.length-1){ index++; mp = MediaPlayer.create(getApplicationContext(), sounds[index]); mp.start(); mp.setOnCompletionListener(this); } else mp.release(); } }); } 
    • Thank you very much for the answer. Please tell me, how is it better to implement a random replay from the list? If the number of playable files is also random. - Pankrashin GO
    • You describe the logic, how you want to do, I will try to answer, not guessing the same sit. - Shwarz Andrei
    • I have already invented an approximate algorithm, only it looks, as it seems to me, not the best way. There are number of repetitions: final int count = new Random().nextInt(25); Made such a condition in the handler for pressing if (index <= count) { index++; int rand = new Random().nextInt(23); mp = MediaPlayer.create(getApplicationContext(), sounds[rand]); mp.start(); mp.setOnCompletionListener(this); } else mp.release(); if (index <= count) { index++; int rand = new Random().nextInt(23); mp = MediaPlayer.create(getApplicationContext(), sounds[rand]); mp.start(); mp.setOnCompletionListener(this); } else mp.release(); - Pankrashin GO
    • Yes, you write in words please. Example: I want a random sound to be selected from the list of my sounds and after that all other sounds will be played in random order, one after the other. So? - Shwarz Andrei
    • A little bit wrong. I want a random number of sounds to be played at random from a list of 25 elements allowed. - Pankrashin GO

    You need:

    1. Remove the loop, call the method once.
    2. Run the next song in onCompletion(MediaPlayer mp)
    • It is understandable to run, but how exactly should I iterate through the audio array? - McDaggen
    • one
      @McDaggen, for example, enter a counter variable in which to store the current position in the audio array. After completing the track, increment it and take the next track by index from the array - YuriySPb