本文介绍了哪种方法最适合在android应用中播放5首歌曲?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的应用程序中必须播放5首歌曲,我在raw
资源文件夹中找到了它们.我该怎么玩?请给我最好的方法.我使用默认播放器还是使用mediaplayer.start()
?我使用5个按钮来显示和播放它们.
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;
}
}
编辑/异常:
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():
}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;
}
推荐答案
您可以执行以下操作:
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()
}
这篇关于哪种方法最适合在android应用中播放5首歌曲?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!