I’m open to suggestions. I’m writing an app that needs a music playing between all activities of the app, and pauses when the user hits ‘home’ or “exits” the app by hitting back and getting out of my app. so here’s what i did – i made a “SoundController” class that contains a static instance of MediaPlayer, like this:
private static MediaPlayer _musicPlayer;
public static void init(Context ctx){
_musicPlayer = new MediaPlayer();
AssetFileDescriptor descriptor;
try {
descriptor = ctx.getAssets().openFd("something.ogg");
_musicPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
_musicPlayer.prepare();
_musicPlayer.setLooping(true);
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
and i call “.start()” and “.pause()” on each activity’s “onResume()” and “onPause()” in my app like this:
@Override
public void onResume(){
System.out.println("got in onResume for mainActivity");
SoundController.playMusic();
super.onResume();
}
@Override
public void onPause(){
System.out.println("got in onPause for mainActivity");
SoundController.pauseMusic();
super.onPause();
}
@Override
public void onDestroy(){
System.out.println("got in onDestroy for mainActivity");
SoundController.tearDown();
super.onDestroy();
}
where the SoundController methods are like:
public static void playMusic(){
if(!_musicPlayer.isPlaying()){
_musicPlayer.start();
}
}
public static void pauseMusic(){
if(_musicPlayer.isPlaying()){
_musicPlayer.pause();
}
}
public static void tearDown(){
if(_musicPlayer.isPlaying()){
_musicPlayer.stop();
_musicPlayer.release();
}
}
but for some reason, i am noticing several bugs like:
- the music keeps playing even after the app exits, if i do things quick enough, like i start the app, hit home really quickly, then start the app again, and hit “back” key to exit the app…
- the app crashes when trying to exit, because for some reason, the onPause() gets called AFTER onDestroy()…. someone please do enlighten me on this, i didn’t even know this was possible. (i’m logging it in my logcat, on kindle fire)
Is it that my static instance of media player is somehow destroying before my app is destroyed?
in the case where the music keeps playing long after my app exits, is it that the “_mediaPlayer.start()” has a delayed start, so it byPasses the check for when the app exits??
sorry for the long post. thanks so much!!!
=======================================================
edit here’s the log for crash:
09-12 14:44:06.304: I/System.out(12191): got in onPause for mainActivity
09-12 14:44:06.366: I/System.out(12191): got in onResume for mainActivity
09-12 14:44:06.726: I/System.out(12191): got in onDestroy for mainActivity
09-12 14:44:07.499: I/System.out(12191): got in onPause for mainActivity
09-12 14:44:07.507: D/AndroidRuntime(12191): Shutting down VM
09-12 14:44:07.507: W/dalvikvm(12191): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-12 14:44:07.507: E/AndroidRuntime(12191): FATAL EXCEPTION: main
09-12 14:44:07.507: E/AndroidRuntime(12191): java.lang.RuntimeException: Unable to pause activity {com.blah/com.blah.mainActivity}: java.lang.IllegalStateException
09-12 14:44:07.507: E/AndroidRuntime(12191): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2354)
09-12 14:44:07.507: E/AndroidRuntime(12191): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2311)
09-12 14:44:07.507: E/AndroidRuntime(12191): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2291)
09-12 14:44:07.507: E/AndroidRuntime(12191): at android.app.ActivityThread.access$1700(ActivityThread.java:117)
09-12 14:44:07.507: E/AndroidRuntime(12191): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:942)
09-12 14:44:07.507: E/AndroidRuntime(12191): at android.os.Handler.dispatchMessage(Handler.java:99)
09-12 14:44:07.507: E/AndroidRuntime(12191): at android.os.Looper.loop(Looper.java:130)
09-12 14:44:07.507: E/AndroidRuntime(12191): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-12 14:44:07.507: E/AndroidRuntime(12191): at java.lang.reflect.Method.invokeNative(Native Method)
09-12 14:44:07.507: E/AndroidRuntime(12191): at java.lang.reflect.Method.invoke(Method.java:507)
09-12 14:44:07.507: E/AndroidRuntime(12191): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:850)
09-12 14:44:07.507: E/AndroidRuntime(12191): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
09-12 14:44:07.507: E/AndroidRuntime(12191): at dalvik.system.NativeStart.main(Native Method)
09-12 14:44:07.507: E/AndroidRuntime(12191): Caused by: java.lang.IllegalStateException
09-12 14:44:07.507: E/AndroidRuntime(12191): at android.media.MediaPlayer.isPlaying(Native Method)
09-12 14:44:07.507: E/AndroidRuntime(12191): at com.blah.controllers.SoundController.pauseMusic(SoundController.java:63)
09-12 14:44:07.507: E/AndroidRuntime(12191): at com.blah.mainActivity.onPause(mainActivity.java:53)
09-12 14:44:07.507: E/AndroidRuntime(12191): at android.app.Activity.performPause(Activity.java:3887)
09-12 14:44:07.507: E/AndroidRuntime(12191): at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1191)
09-12 14:44:07.507: E/AndroidRuntime(12191): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2341)
09-12 14:44:07.507: E/AndroidRuntime(12191): ... 12 more
Okay, the key here was the Kindle Fire & WarrenFaith’s comment on my question.
STUPIDLY enough, on Kindle Fire version 1, the activity cycle is sometimes messed up – onPause gets called after onDestroy is called. it makes no sense. in my onDestroy(), i tear down the entire SoundController variables because the onDestroy from my main activity means the user “exited” my app.
so what happened was that the onPause gets called after the media player is dead. of course, by then, it’s in an illegal state.
this bug is not reproducible on my two other android devices. (true blue android, not a forked version of it, like kindle fire)