Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8663533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:00:34+00:00 2026-06-12T17:00:34+00:00

I’m open to suggestions. I’m writing an app that needs a music playing between

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:00:35+00:00Added an answer on June 12, 2026 at 5:00 pm

    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)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am writing an app with both english and french support. The app requests
Let's say I'm outputting a post title and in our database, it's Hello Y’all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a small JavaScript validation script that validates inputs based on Regex. I
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.