2

When I am out of my app (when pressed home button or back button), the music keeps playing in the background. How can I stop it?

public class MainActivity extends Activity implements OnClickListener {

MediaPlayer ourSong;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ourSong = MediaPlayer.create(MainActivity.this, R.raw.girissong);
    ourSong.start();

    // codes

}

@Override
public void onClick(View v) {

    // some code

         }

         }

}

I made this code and it should stop when I go to next class and the next song will start

double-beep
  • 5,031
  • 17
  • 33
  • 41
berk kaan
  • 447
  • 2
  • 7
  • 12

5 Answers5

4

Override the onPause method of the activity and call stop on the MediaPlayer.

cjk
  • 45,739
  • 9
  • 81
  • 112
4

Place the below two methods in you class for stopping the Music play while navigating to other Screens and Home button respectively....

@Override
public void onPause()
{
   super.onPause();
   if(ourSong.isPlaying())
   ourSong.stop();
   else
   return;
}

@Override
public void onStop()
{
   super.onStop();
  if(ourSong.isPlaying())
    ourSong.stop();
  else
   return;
}
Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
  • I did that and it forces to close – berk kaan Dec 20 '12 at 12:44
  • FATAL EXCEPTION : main java.lang.Runtimeexception:unable to pause activity(com.example.MainActivity). java.langI11 Legal.state.exception – berk kaan Dec 20 '12 at 13:24
  • 12-20 13:21:49.882: E/AndroidRuntime(1186): FATAL EXCEPTION: main 12-20 13:21:49.882: E/AndroidRuntime(1186): java.lang.RuntimeException: Unable to pause activity {com.example.barbaryaman/com.example.barbaryaman.MainActivity}: java.lang.IllegalStateException 12-20 13:21:49.882: E/AndroidRuntime(1186): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3016) 12-20 13:21:49.882: E/AndroidRuntime(1186): at dalvik.system.NativeStart.main(Native Method) – berk kaan Dec 20 '12 at 13:28
  • 12-20 13:21:49.882: E/AndroidRuntime(1186): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2971) 12-20 13:21:49.882: E/AndroidRuntime(1186): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2949) 12-20 13:21:49.882: E/AndroidRuntime(1186): at android.app.ActivityThread.access$800(ActivityThread.java:141) – berk kaan Dec 20 '12 at 13:29
  • 12-20 13:21:49.882: E/AndroidRuntime(1186): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1245) 12-20 13:21:49.882: E/AndroidRuntime(1186): at android.os.Handler.dispatchMessage(Handler.java:99) 12-20 13:21:49.882: E/AndroidRuntime(1186): at android.os.Looper.loop(Looper.java:137) 12-20 13:21:49.882: E/AndroidRuntime(1186): at android.app.ActivityThread.main(ActivityThread.java:5039) 12-20 13:21:49.882: E/AndroidRuntime(1186): at java.lang.reflect.Method.invokeNative(Native Method) – berk kaan Dec 20 '12 at 13:29
  • 12-20 13:21:49.882: E/AndroidRuntime(1186): at java.lang.reflect.Method.invoke(Method.java:511) 12-20 13:21:49.882: E/AndroidRuntime(1186): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 12-20 13:21:49.882: E/AndroidRuntime(1186): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 12-20 13:21:49.882: E/AndroidRuntime(1186): at dalvik.system.NativeStart.main(Native Method) 12-20 13:21:49.882: E/AndroidRuntime(1186): Caused by: java.lang.IllegalStateException – berk kaan Dec 20 '12 at 13:29
  • forgot to call super.onPause() and super.onStop() methods. I have edited the answer. Please use that. Now wont get the error – Avadhani Y Dec 20 '12 at 13:32
  • i still got errors, i guess i will try using exit button. thanks for your help but didnt work for me , i give up :) – berk kaan Dec 20 '12 at 13:49
  • I dont know where u r getting errors.. Dont give up man... actually what i understood is - 1. while clicking the HOME and 2. While navigating to other screens the music is not stopping... thats it right? So use onPause() and onStop() for HOME and other screens respectively... – Avadhani Y Dec 20 '12 at 13:59
  • it stops when i go to other screen because i use ourSong.release(); but it doesnt stop when i press home – berk kaan Dec 20 '12 at 16:04
  • 1
    I guess i was putting that code in a wrong place so i was getting errors, i did it right at the bottom of my code and it worked fine now . thanks a lot , you are master :) – berk kaan Dec 20 '12 at 16:20
2

Check out what you want to do on onPause. Example:

protected void onPause() {
     super.onPause();

     ourSong.stop();
 }
David Olsson
  • 8,085
  • 3
  • 30
  • 38
2

You have to override onKeyDown() method and check the keyevent if it is home key or back button methong, and inside this method stop your media player object then release it. mp.stop(); mp.release();

Mohamed
  • 761
  • 9
  • 19
1

you should probably put

if (player != null && player.isPlaying()) {
    player.stop();
    player.release();
    player = null;
}

inside your onStop(), or onDestroy() method of your activity also.

If you put it in onStop() the audio would stop when the home button is pressed(or anytime another activity takes over phone rings perhaps).

If you put it in onDestroy() it would keep playing until the system needs the resources that your application is using. Either way it is a good idea so that your app is not holding on the the MediaPlayer resources incase the user doesn't hit the stop button for whatever reason.

Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78