Note that there are some explanatory texts on larger screens.

plurals
  1. POwhy does a static MediaPlayer instance not work for Android?
    text
    copied!<p>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:</p> <pre><code>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(); } } </code></pre> <p>and i call ".start()" and ".pause()" on each activity's "onResume()" and "onPause()" in my app like this:</p> <pre><code>@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(); } </code></pre> <p>where the SoundController methods are like:</p> <pre><code>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(); } } </code></pre> <p>but for some reason, i am noticing several bugs like:</p> <ul> <li>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...</li> <li>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)</li> </ul> <p>Is it that my static instance of media player is somehow destroying before my app is destroyed? </p> <p>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??</p> <p>sorry for the long post. thanks so much!!!</p> <p>=======================================================</p> <p><em>edit</em> here's the log for crash:</p> <pre><code>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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload