1

I created an android app which plays a sound file. When I press the home button I noticed that the sound continues to play and does not stop so I added the following function inside my Class:

@Override
protected void onStop()
{
    super.onStop();
    finish(); //kill the thread
    pS.reset(); //stop the sound
}
@Override
protected void onPause()
{
    super.onPause();
    finish(); //kill the thread
    pS.reset(); //stop the sound
}

Which does the job when "Back" or "Home" is pressed, it stops the music and the app does not force close. The only issue is, the sound has to be playing for it to not FC. If there is no sound playing, the app FC. I am guessing it's the

pS.reset();

function not knowing what to do when the sound isn't playing.

If i set it to:

pS = null;

rather than pS.reset(), will it do the same without FC when "Back" or "Home" is pressed?

My pS initialization:

pS = MediaPlayer.create(iAct.this, R.drawable.sound);

EDIT: I commented out

pS.reset();

and added

pS = null;

it does not FC but the sound does not stop playing.

I updated my onPause():

@Override
protected void onPause()
{
    super.onPause();
    if (pS.isPlaying()) {
        pS.pause(); //stop the sound
        finish();
    }
    else
        finish();
}

My Log:

04-02 13:06:40.563: E/AndroidRuntime(925): FATAL EXCEPTION: main
04-02 13:06:40.563: E/AndroidRuntime(925): java.lang.RuntimeException: Unable to pause activity {com.testcom.myapp/com.testcom.myapp.MyappActivity}: java.lang.NullPointerException
04-02 13:06:40.563: E/AndroidRuntime(925):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2354)
04-02 13:06:40.563: E/AndroidRuntime(925):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2311)
04-02 13:06:40.563: E/AndroidRuntime(925):  at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2291)
04-02 13:06:40.563: E/AndroidRuntime(925):  at android.app.ActivityThread.access$1700(ActivityThread.java:117)
04-02 13:06:40.563: E/AndroidRuntime(925):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:938)
04-02 13:06:40.563: E/AndroidRuntime(925):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-02 13:06:40.563: E/AndroidRuntime(925):  at android.os.Looper.loop(Looper.java:123)
04-02 13:06:40.563: E/AndroidRuntime(925):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-02 13:06:40.563: E/AndroidRuntime(925):  at java.lang.reflect.Method.invokeNative(Native Method)
04-02 13:06:40.563: E/AndroidRuntime(925):  at java.lang.reflect.Method.invoke(Method.java:507)
04-02 13:06:40.563: E/AndroidRuntime(925):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-02 13:06:40.563: E/AndroidRuntime(925):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-02 13:06:40.563: E/AndroidRuntime(925):  at dalvik.system.NativeStart.main(Native Method)
04-02 13:06:40.563: E/AndroidRuntime(925): Caused by: java.lang.NullPointerException
04-02 13:06:40.563: E/AndroidRuntime(925):  at com.testcom.myapp.MyappActivity.onPause(MyappActivity.java:119)
04-02 13:06:40.563: E/AndroidRuntime(925):  at android.app.Activity.performPause(Activity.java:3851)
04-02 13:06:40.563: E/AndroidRuntime(925):  at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1191)
04-02 13:06:40.563: E/AndroidRuntime(925):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2341)
04-02 13:06:40.563: E/AndroidRuntime(925):  ... 12 more
Si8
  • 9,141
  • 22
  • 109
  • 221
  • First of all: What's your Logcat output? It should point you directly to the source of your FC. Secondly: I hope that's a typo, but you're calling super.onStop() in your override of onPause() !! This is NOT right, you'd have to call super.onPause() in your onPause override. Furthermore: Why do you finish your Activity in onPause/onStop? – AgentKnopf Apr 02 '13 at 12:16
  • I changed to super.onPause(); – Si8 Apr 02 '13 at 12:18
  • Ok, what I am looking to do is HOME or BACK is pressed, it should stop the music from playing, if it's playing. – Si8 Apr 02 '13 at 12:19

3 Answers3

1

you can put PS.stop in onPause().

Mohan
  • 651
  • 4
  • 13
  • onStop is for HOME and onPause for BACK... right? Tried it and it did not work :( – Si8 Apr 02 '13 at 12:15
  • Back is for onPause, after some time it will goes to onStop, when you click on back button then u need to stop ur music then add Ps.stop in onPause() only. – Mohan Apr 02 '13 at 12:18
  • 1
    That's not necessarily true - you might want to read up on the activity life cycle at: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle – Martin Apr 02 '13 at 12:18
  • Ok, what I am looking to do is HOME or BACK is pressed, it should stop the music from playing, if it's playing. – Si8 Apr 02 '13 at 12:19
1

just put into the onPause() because all the method like onStop() / onDestroy() are called after onPause() only that was the flow of life cycle of Activity so just write only in onPause.

@Override
protected void onPause()
{
    if(ps.isPlaying)
       pS.stop(); //stop the sound

    super.onStop();
    finish(); //kill the thread
}

ok for different thing like need to implement for back and home button then implement onKeyUp/onKeyDown and detect the key and perform pause/stop for your current play track

Pratik
  • 30,639
  • 18
  • 84
  • 159
  • It's funny, I just added the code before you posted and it worked like a charm! Thanks :) So, I can take out the onStop method completely? – Si8 Apr 02 '13 at 12:27
  • I added your code but like before if the sound isn't playing, it FC When I press HOME/BACK. Any idea? – Si8 Apr 02 '13 at 12:36
1

Take a look here might helps you with your issue:

How to mute and unmute it on the onPause and onResumee

With that you can pause and start it where it stopped.

Like this:

Global:

MediaPlayer ourSong;

In your OnCreate

ourSong = MediaPlayer.create(yourActivity.this, YourSong);
        ourSong.setLooping(true);
ourSong.start();

In you pause function

    @Override
protected void onPause()
{
    super.onPause();
    if(ourSong.isPlaying)
    ourSong.pause(); //stop the sound
}

@Override
protected void onResume() 
{
    super.onResume();
    ourSong.start();
}
Community
  • 1
  • 1
Guilherme Gregores
  • 1,050
  • 2
  • 10
  • 27