My music player application seems to have a problem. When I exit from it, it pops out force close warning. This is the part of the code where I think might be the cause of the problem after debugging it:
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
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);
}
};
And here's the logcat:
12-13 13:26:01.700: E/AndroidRuntime(31838): FATAL EXCEPTION: main
12-13 13:26:01.700: E/AndroidRuntime(31838): java.lang.IllegalStateException
12-13 13:26:01.700: E/AndroidRuntime(31838): at android.media.MediaPlayer.getDuration(Native Method)
12-13 13:26:01.700: E/AndroidRuntime(31838): at com.example.musicshare.AndroidBuildingMusicPlayerActivity$1.run(AndroidBuildingMusicPlayerActivity.java:341)
12-13 13:26:01.700: E/AndroidRuntime(31838): at android.os.Handler.handleCallback(Handler.java:587)
12-13 13:26:01.700: E/AndroidRuntime(31838): at android.os.Handler.dispatchMessage(Handler.java:92)
12-13 13:26:01.700: E/AndroidRuntime(31838): at android.os.Looper.loop(Looper.java:123)
12-13 13:26:01.700: E/AndroidRuntime(31838): at android.app.ActivityThread.main(ActivityThread.java:3691)
12-13 13:26:01.700: E/AndroidRuntime(31838): at java.lang.reflect.Method.invokeNative(Native Method)
12-13 13:26:01.700: E/AndroidRuntime(31838): at java.lang.reflect.Method.invoke(Method.java:507)
12-13 13:26:01.700: E/AndroidRuntime(31838): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
12-13 13:26:01.700: E/AndroidRuntime(31838): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
12-13 13:26:01.700: E/AndroidRuntime(31838): at dalvik.system.NativeStart.main(Native Method)
The problem is with mp.getDuration I think but I dont know how to fix it.
Looks like your runnable task still runs at the back even after you exit. So my gues is that you could have forgot to call the removeCallbacks()
.
My suggestion is to add the below line in your onBackPressed()
or at the place where you exit the app,
mHandler.removeCallbacks(mUpdateTimeTask);
Make sure you declare your Runnable mUpdateTimeTask globally so that it is available through out your code.