title: which way is the best for playing 5 songs in android app?
body: <p>I have 5 songs that must be played in my app, I locate them in <code>raw</code> resource folder. how can I play them? please give me the best way. I use default player or use <code>mediaplayer.start()</code>?
  I use 5 buttons to show them and play them.</p>
  
  <pre><code>public class MusicActivity extends AppCompatActivity implements View.OnClickListener{
  private MediaPlayer a,b,c,d,e;
  private Button aBTN,bBTN,cBTN, dBTN ,fBTN;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_music);
  
      a = MediaPlayer.create(getApplicationContext(), R.raw.a);
      b = MediaPlayer.create(getApplicationContext(), R.raw.b);
      c = MediaPlayer.create(getApplicationContext(), R.raw.c);
      d = MediaPlayer.create(getApplicationContext(), R.raw.d);
      e = MediaPlayer.create(getApplicationContext(), R.raw.f);
      aBTN = (Button)findViewById(R.id.a);
      bBTN = (Button)findViewById(R.id.b);
      cBTN = (Button)findViewById(R.id.c);
      dBTN = (Button)findViewById(R.id.d);
      fBTN = (Button)findViewById(R.id.e);
  
  }
  
  @Override
  public void onClick(View view) {
     switch (view.getId()){
         case R.id.a:
             if (b.isPlaying() || d.isPlaying() || e.isPlaying()){
                 b.pause();       d.pause();       e.pause();
                 a.start();
             } else a.pause();
             break;
  
         case R.id.b:
             if (a.isPlaying() || d.isPlaying() || e.isPlaying()){
                 a.pause();       d.pause();       e.pause();
                 b.start();
             } else b.pause();
             break;
  
         case R.id.d:
            // if (a.isPlaying() || b.isPlaying() || e.isPlaying()){
              //   a.pause();       b.pause();       e.pause();
                 d.start();
           //  } else d.pause();
             break;
  
         case R.id.e:
             if (a.isPlaying() || b.isPlaying() || d.isPlaying()){
                 a.pause();       b.pause();       d.pause();
                 e.start();
             } else e.pause();
             break;
     }
  }
  </code></pre>
  
  <p>EDIT/Exception :</p>
  
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
                Process: , PID: 2583
                java.lang.IllegalStateException: Could not execute method for android:onClick
                    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                    at android.view.View.performClick(View.java:6199)
                    at android.widget.TextView.performClick(TextView.java:11090)
                    at android.view.View$PerformClick.run(View.java:23647)
                    at android.os.Handler.handleCallback(Handler.java:751)
                    at android.os.Handler.dispatchMessage(Handler.java:95)
                    at android.os.Looper.loop(Looper.java:154)
                    at android.app.ActivityThread.main(ActivityThread.java:6682)
                    at java.lang.reflect.Method.invoke(Native Method)
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
                 Caused by: java.lang.reflect.InvocationTargetException
                    at java.lang.reflect.Method.invoke(Native Method)
                    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                    at android.view.View.performClick(View.java:6199) 
                    at android.widget.TextView.performClick(TextView.java:11090) 
                    at android.view.View$PerformClick.run(View.java:23647) 
                    at android.os.Handler.handleCallback(Handler.java:751) 
                    at android.os.Handler.dispatchMessage(Handler.java:95) 
                    at android.os.Looper.loop(Looper.java:154) 
                    at android.app.ActivityThread.main(ActivityThread.java:6682) 
                    at java.lang.reflect.Method.invoke(Native Method) 
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 
                 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.stop()' on a null object reference
                    at MusicActivity.handlePlayer(MusicActivity.java:70)
                    at maxsoftapps.com.MusicActivity.onClick(MusicActivity.java:50)
                    at java.lang.reflect.Method.invoke(Native Method) 
                    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                    at android.view.View.performClick(View.java:6199) 
                    at android.widget.TextView.performClick(TextView.java:11090) 
                    at android.view.View$PerformClick.run(View.java:23647) 
                    at android.os.Handler.handleCallback(Handler.java:751) 
                    at android.os.Handler.dispatchMessage(Handler.java:95) 
                    at android.os.Looper.loop(Looper.java:154) 
                    at android.app.ActivityThread.main(ActivityThread.java:6682) 
                    at java.lang.reflect.Method.invoke(Native Method) 
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 
  </code></pre>
  
  <p>EDIT:</p>
  
  <pre><code>public class MusicActivity extends AppCompatActivity implements View.OnClickListener{
  private MediaPlayer a,b,d,f,currentMedia;
  private Button aBTN,bBTN, dBTN ,fBTN;
  private int preCurrentDataSource =0;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_music);
  
      a = MediaPlayer.create(getApplicationContext(), R.raw.a);
      b = MediaPlayer.create(this, R.raw.b);
      d = MediaPlayer.create(this, R.raw.d);
      f = MediaPlayer.create(this, R.raw.f);
      aBTN = (Button)findViewById(R.id.a);
      bBTN = (Button)findViewById(R.id.b);
      dBTN = (Button)findViewById(R.id.d);
      fBTN = (Button)findViewById(R.id.e);
  
  }
  
  @Override
  public void onClick(View view) {
     switch (view.getId()){
         case R.id.a:
            handlePlayer(R.raw.a);
  
             break;
  
         case R.id.b:
             handlePlayer(R.raw.b);
  
             break;
  
         case R.id.d:
             handlePlayer(R.raw.d);
  
             break;
  
         case R.id.e:
             handlePlayer(R.raw.f);
  
             break;
     }
  }
  
  private void handlePlayer(int currentDataSource) {
      currentMedia=MediaPlayer.create(this,currentDataSource);
      if (preCurrentDataSource != currentDataSource) {
          currentMedia.stop();
          currentMedia.release();
          currentMedia = MediaPlayer.create(this, currentDataSource);
          currentMedia.start();
          preCurrentDataSource= currentDataSource;
      } else {
          if (currentMedia.isPlaying())
              currentMedia.pause();
          else
              currentMedia.start();
      }
  
  
  }
  
  
  
  public void onStop() {
      super.onStop();
      currentMedia.stop();
      currentMedia.release();
      currentMedia.stop();
      currentMedia.release();
      finish();
  }
  </code></pre>
  
  <p>}
  EDIT handelerPalying():</p>
  
  <pre><code>private void handlePlayer(int currentDataSource) {
      if (preCurrentDataSource == 0){
          currentMedia = MediaPlayer.create(this,currentDataSource);
          currentMedia.start();
          preCurrentDataSource=currentDataSource;
      } else {
          currentMedia.stop();
          currentMedia.release();
          currentMedia=MediaPlayer.create(this,currentDataSource);
          currentMedia.start();
          preCurrentDataSource=currentDataSource;
      }
  </code></pre>