title: How do I play multiple audio files multiple times in android?
body: <p>I have 5 audio files a,b,c,d,e</p>
  
  <p>When the user clicks the play button it should play audio files from a to e one after another.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>public class MainActivity extends AppCompatActivity {
  
  
  
      Button play,stop;
  
      MediaPlayer mp;
      MediaPlayer mp2;
      MediaPlayer mp3;
      MediaPlayer mp4;
      MediaPlayer mp5;
  
  
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          play = findViewById(R.id.play);
          stop = findViewById(R.id.stop);
      }
  
  
  
  
      public void Play(View view){
          StopPlaying();
          mp= MediaPlayer.create(MainActivity.this,R.raw.a);
          mp2= MediaPlayer.create(MainActivity.this,R.raw.b);
          mp3= MediaPlayer.create(MainActivity.this,R.raw.c);
          mp4= MediaPlayer.create(MainActivity.this,R.raw.d);
          mp5= MediaPlayer.create(MainActivity.this,R.raw.e);
          int i = 0;
          while (i&lt;10){
              i++;
              mp.start();
              mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                  @Override
                  public void onCompletion(MediaPlayer mp) {
                      mp2.start();
                      mp2.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                          @Override
                          public void onCompletion(MediaPlayer mp) {
                              mp3.start();
                              mp3.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                                  @Override
                                  public void onCompletion(MediaPlayer mp) {
                                      mp4.start();
                                      mp4.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                                          @Override
                                          public void onCompletion(MediaPlayer mp) {
                                              mp5.start();
                                              mp5.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                                                  @Override
                                                  public void onCompletion(MediaPlayer mp) {
  
                                                  }
                                              });
                                          }
                                      });
                                  }
                              });
                          }
                      });
                  }
              });
              
          }
      }
  
  
  
  
  
      
      
      
  
      private void StopPlaying(){
  
          if (mp != null) {
              mp.stop();
              mp.release();
              mp = null;
          }
  
  
          if (mp2 != null) {
              mp2.stop();
              mp2.release();
              mp2 = null;
          }
  
          if (mp3 != null) {
              mp3.stop();
              mp3.release();
              mp3 = null;
          }
  
  
          if (mp4 != null) {
              mp4.stop();
              mp4.release();
              mp4 = null;
          }
  
  
          if (mp5 != null) {
              mp5.stop();
              mp5.release();
              mp5 = null;
          }
  
  
      }
  
      public void Stop(View viw){
          mp.stop();
          mp2.stop();
          mp3.stop();
          mp4.stop();
          mp5.stop();
      }
  
  
  
  
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;LinearLayout android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      android:background="#afc3d7"
      xmlns:android="http://schemas.android.com/apk/res/android"&gt;
  
  
  
      &lt;EditText
          android:id="@+id/from"
          android:layout_width="wrap_content"
          android:layout_height="50dp"
          android:textStyle="bold"
          android:ems="10"
          android:inputType="number"
           /&gt;
  
  
  
      &lt;EditText
          android:id="@+id/to"
          android:layout_width="wrap_content"
          android:layout_height="50dp"
          android:textStyle="bold"
          android:ems="10"
          android:inputType="number"
           /&gt;
  
  
      &lt;EditText
          android:id="@+id/times"
          android:layout_width="wrap_content"
          android:layout_height="50dp"
          android:ems="10"
          android:textStyle="bold"
          android:inputType="number"
          /&gt;
  
  
  
      &lt;Button
          android:id="@+id/play"
          android:layout_width="175dp"
          android:layout_height="50dp"
          android:onClick="Play"
          android:text="Play" /&gt;
  
  
      &lt;Button
          android:id="@+id/stop"
          android:layout_width="175dp"
          android:onClick="Stop"
          android:layout_height="50dp"
          android:text="Stop" /&gt;
  
  
  
  
  &lt;/LinearLayout&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>But what I want is it should play them like 10 times,</p>
  
  <p>(after e is played, it should again start from a toe)</p>
  
  <p>like from a toe and then again from a toe and then again.....repeat this 10 times</p>
  
  <p>It's like repeating a song in a music player once it is finished, the only difference is I am repeating multiple audio files in a sequence.</p>
  
  <p>I have tried using for loop and while loop but none of them seem to work...</p>
  
  <p>Here is my code</p>