问题
I have 5 songs that must be played in my app, I locate them in raw
resource folder. how can I play them? please give me the best way. I use default player or use mediaplayer.start()
?
I use 5 buttons to show them and play them.
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)
EDIT:
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();
}
} EDIT 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;
}
回答1:
you can do something like this:
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()
}
来源:https://stackoverflow.com/questions/59809009/which-way-is-the-best-for-playing-5-songs-in-android-app