[英]FATAL EXCEPTION: main java.lang.IllegalStateException When press Back
I have Runnable that runs in Thread in a MediaPlayer. 我有在MediaPlayer中的Thread中运行的Runnable。 When i press back I get an IllegalStateException.
当我按回时,我得到一个IllegalStateException。
Here is the Runnable: 这是Runnable:
mp is MediaPlayer mp是MediaPlayer
private Runnable mUpdateTimeTask = new Runnable() {
public void run() //This line is AndroidBuildingMusicPlayerActivity.java:318
{
long totalDuration = mp.getDuration();
long currentDuration = mp.getCurrentPosition();
// Displaying Total Duration time
songTotalDurationLabel.setText(""+utils.milliSecondsToTimer(totalDuration));
// Displaying time completed playing
songCurrentDurationLabel.setText(""+utils.milliSecondsToTimer(currentDuration));
// Updating progress bar
int progress = (int)(utils.getProgressPercentage(currentDuration, totalDuration));
//Log.d("Progress", ""+progress);
songProgressBar.setProgress(progress);
// Running this thread after 100 milliseconds
mHandler.postDelayed(this, 100);
}
};
OnDetroy method: OnDetroy方法:
@Override
public void onDestroy(){
super.onDestroy();
mp.release();
}
Error log: 错误日志:
FATAL EXCEPTION: main
java.lang.IllegalStateException
at android.media.MediaPlayer.getDuration(Native Method)
at com.androidhive.musicplayer.AndroidBuildingMusicPlayerActivity$9.run(AndroidBuildingMusicPlayerActivity.java:318)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
I suspect that you called mp.pause()
in OnPause()
method of this activity. 我怀疑您在此活动的
OnPause()
方法中调用了mp.pause()
。 So when you clicked back button, MediaPayer went into pause status. 因此,当您单击“后退”按钮时,MediaPayer进入了暂停状态。 However since your Runnable is called asynchronous, so it will call mp.getDuration() in an invalid state.
但是,由于您的Runnable称为异步,因此它将在无效状态下调用mp.getDuration()。 To fix this, please add in the check:
要解决此问题,请添加检查:
if (mp.isPlaying()) {
long totalDuration = mp.getDuration();
long currentDuration = mp.getCurrentPosition();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.