哪种方式最适合在 android 应用中播放 5 首歌曲?

which way is the best for playing 5 songs in android app?

我有 5 首歌曲必须在我的应用程序中播放,我将它们放在 raw 资源文件夹中。我怎么玩?请给我最好的方法。我使用默认播放器还是使用 mediaplayer.start()? 我用5个按钮来显示和播放它们。

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;
   }
}

EDIT/Exception :

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) 

编辑:

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();
}

} 编辑 handelerPalying():

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;
    }

你可以这样做:

private lateinit var currentMedia: MediaPlayer
private var currentDataSource: Int = 0

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val play: Button = findViewById(R.id.play)
    val play2: Button = findViewById(R.id.play2)

    currentMedia = MediaPlayer.create(this, R.raw.a)

    play.setOnClickListener(this)
    play2.setOnClickListener(this)
}

override fun onClick(v: View) {
    when (v.id) {
        R.id.play -> handlePlayer(R.raw.a)
        R.id.play2 -> handlePlayer(R.raw.b)
    }
}

private fun handlePlayer(dataSource: Int) {
    if (currentDataSource != dataSource) {
        currentMedia.stop()
        currentMedia.release()
        currentMedia = MediaPlayer.create(this, dataSource)
        currentMedia.start()
        currentDataSource = dataSource
    } else {
        if (currentMedia.isPlaying)
            currentMedia.pause()
        else
            currentMedia.start()
    }
}

override fun onDestroy() {
    super.onDestroy()
    currentMedia.stop()
    currentMedia.release()
}