0

I have the following code which plays some audio after some time in a service. The issue is, after exiting the app, the media player continues to run; I want to stop the audio from playing after the app has has been exited or closed.

enter image description here

By the way, ten and three were declared at the top:

MediaPlayer ten; MediaPlayer three;

Does some piece of code run every time an app is closed? If so, I could call stopService() from that piece of code.

I would really appreciate any feedback (positive or negative)! Thank you so much for all of your help, let me know if you need any more code.

{Rich}
Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83

4 Answers4

1

onStop you can mute player and

onResume you can play music player.

OnDestroy you can stop your service

stopService(new Intent(YourService));

For reference mute and unmute player

How to mute MediaPlayer in android

Community
  • 1
  • 1
Parag Chauhan
  • 35,760
  • 13
  • 86
  • 95
0

Yes, Android have a lifecycle that run the onCreate, onPause, onStop and onDestory. When you exit the app and wish to stop the player, you might want to do something in the onStop.

You can look at the lifecycle at http://developer.android.com/training/basics/activity-lifecycle/stopping.html

Cheers

John
  • 49
  • 6
  • Thanks John! Is there a way that I can mute it in the on pause? How do I mute my android app until the user comes back? Thanks so much, I will mark you best answer(+15 rep) and like your post (+5 rep) once I get this figured out! – Ruchir Baronia Nov 05 '15 at 04:53
  • Well, you can use the setVolume function in the Mediaplayer API and set it to setVolume(0,0), look at http://developer.android.com/reference/android/media/MediaPlayer.html#setVolume(float,%20float) for reference – John Nov 05 '15 at 05:05
  • That will change the entire devices volume, which users may dislike. How can I mute **only** my app? – Ruchir Baronia Nov 05 '15 at 05:15
  • If that is the case, why not just use .pause() ? – John Nov 05 '15 at 05:25
  • Nevermind, I understand what you were saying. I set the mediaplayer volume, but am getting a null pointer exception. Any idea why? – Ruchir Baronia Nov 05 '15 at 05:27
  • ten.setVolume(0, 0); three.setVolume(0, 0); – Ruchir Baronia Nov 05 '15 at 05:27
  • public static void mute(){ ten.setVolume(0, 0); three.setVolume(0, 0); } public static void unMute(){ ten.setVolume(1, 1); three.setVolume(1, 1); } – Ruchir Baronia Nov 05 '15 at 05:28
  • TwentySeconds.unMute(); and TwentySeconds.mute(); – Ruchir Baronia Nov 05 '15 at 05:29
  • your ten and three variable should be null that cause that to happen, it depends when you call those. Do a trace in debug mode to see what's going on. – John Nov 05 '15 at 05:41
0

If you have one activity that is always the task root and only the task root, overriding onBackPressed in that activity is probably the best way to tell if the app is "exiting" as far as the user is concerned. You would not want to kill the process when the activity is merely sent to the background.

Kevin Krumwiede
  • 9,868
  • 4
  • 34
  • 82
  • What do you suggest doing for multiple activities – Ruchir Baronia Nov 05 '15 at 03:49
  • @Rich If you have multiple activities, you haven't exited the app. – Kevin Krumwiede Nov 05 '15 at 03:50
  • The thing is, the user is always switching between activities. If they decide to press the home button, I want to shut all processes. How can I do that? I don't have control over **where** the user will exit the app, but **wherever** he/she does, I want to exit all processes. – Ruchir Baronia Nov 05 '15 at 03:53
  • @Rich "If they decide to press the home button, I want to shut all processes." - That's a great way to piss off your users. If the user presses home, the app should remain running. (Or at least it should appear that way to the user.) – Kevin Krumwiede Nov 05 '15 at 03:59
  • I agree, the thing is, it would piss them off even more if there were random sounds running every few seconds while they are trying to use another app. Is there a way I can mute my app until they come back? I will mark you best answer and like your post as soon as I get this to work. Thanks! :) – Ruchir Baronia Nov 05 '15 at 04:02
  • @Rich If you want the sound to stop when the activity is not visible, mute it in `onPause()` or `onStop()`. See the link in John's answer. – Kevin Krumwiede Nov 05 '15 at 04:05
  • @Rich That calls for a separate question. – Kevin Krumwiede Nov 05 '15 at 04:10
0

Thing you needed to check is when app goes in background and when app goes in background stop your service. For that you needed use

ActivityLifecycleCallbacks

You can find more info about from here ActivityLifecycleCallbacks

Also an example which will help you. link

Community
  • 1
  • 1
shreyash mashru
  • 301
  • 2
  • 9