我的代码:
llBtn = (LinearLayout) findViewById(R.id.button);
llBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(MainActivity.this, "LL WAS CLICKED", Toast.LENGTH_SHORT).show();
//IF AUDIO IS NOT PLAYING... PLAY AUDIO... OTHERWISE... STOP AUDIO
if (tvPS.getText() == "Stop!") {
StopAudio();
}
else {
PlayAudio();
}
}
});
public void StopAudio() {
if (mediaPlayer.isPlaying()) {
stopPlaying(mediaPlayer);
}
Toast.makeText(MainActivity.this, "STOPPED", Toast.LENGTH_SHORT).show();
tvPS.setText("Play!");
}
public void PlayAudio() {
stopPlaying(mediaPlayer);
tvPS.setText("Stop!");
//Toast.makeText(MainActivity.this, "1", Toast.LENGTH_SHORT).show();
inResId = getResources().getIdentifier("myaudio2" , "raw", getPackageName());
mediaPlayer = MediaPlayer.create(getBaseContext(), inResId);
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
//Toast.makeText(MainActivity.this, "Done", Toast.LENGTH_SHORT).show();
}
});
}
private void stopPlaying(MediaPlayer mp) {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
当我播放音频并在中间停止并再次点击播放时,应用程序会崩溃并显示以下logcat:
04-06 18:46:48.383: E/AndroidRuntime(13041): FATAL EXCEPTION: main
04-06 18:46:48.383: E/AndroidRuntime(13041): Process: com.myapp, PID: 13041
04-06 18:46:48.383: E/AndroidRuntime(13041): java.lang.IllegalStateException
04-06 18:46:48.383: E/AndroidRuntime(13041): at android.media.MediaPlayer._stop(Native Method)
04-06 18:46:48.383: E/AndroidRuntime(13041): at android.media.MediaPlayer.stop(MediaPlayer.java:1211)
04-06 18:46:48.383: E/AndroidRuntime(13041): at com.myapp.MainActivity.stopPlaying(MainActivity.java:131)
04-06 18:46:48.383: E/AndroidRuntime(13041): at com.myapp.MainActivity.PlayAudio(MainActivity.java:83)
04-06 18:46:48.383: E/AndroidRuntime(13041): at com.myapp.MainActivity$1.onClick(MainActivity.java:52)
04-06 18:46:48.383: E/AndroidRuntime(13041): at android.view.View.performClick(View.java:4756)
04-06 18:46:48.383: E/AndroidRuntime(13041): at android.view.View$PerformClick.run(View.java:19761)
04-06 18:46:48.383: E/AndroidRuntime(13041): at android.os.Handler.handleCallback(Handler.java:739)
04-06 18:46:48.383: E/AndroidRuntime(13041): at android.os.Handler.dispatchMessage(Handler.java:95)
04-06 18:46:48.383: E/AndroidRuntime(13041): at android.os.Looper.loop(Looper.java:135)
04-06 18:46:48.383: E/AndroidRuntime(13041): at android.app.ActivityThread.main(ActivityThread.java:5253)
04-06 18:46:48.383: E/AndroidRuntime(13041): at java.lang.reflect.Method.invoke(Native Method)
04-06 18:46:48.383: E/AndroidRuntime(13041): at java.lang.reflect.Method.invoke(Method.java:372)
04-06 18:46:48.383: E/AndroidRuntime(13041): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
04-06 18:46:48.383: E/AndroidRuntime(13041): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
如何解决问题。
我希望能够再次播放和停止播放。
答案 0 :(得分:3)
根据documentation,stop()
将MediaPlayer置于"停止"州。在此状态下不允许调用start()
,因此例外。
从此状态再次播放的唯一方法是拨打prepare
或prepareAsync
,等待onPrepared
,最后拨打start()
。
相反,您可以反复pause()
和start()
。
由于您只播放音频,因此更换以下内容可能有意义:
mp.stop();
mp.release();
使用:
mp.pause();
mp.seekTo(0);
我注意到您的PlayAudio
创建了MediaPlayer
的新实例。如果您将上述解决方案与pause
一起使用,则应创建MediaPlayer
一次(例如在onStarted
中)并且不要将其发布到onStopped
。