2

I "created" an application and when I go back or I press the home button to exit from the application, the audio from my application is still running! I have to close all running apps to make it stop.

How can I stop the audio running when I press the home button?

Tiago Almeida
  • 14,081
  • 3
  • 67
  • 82
Gil Ralph
  • 21
  • 1
  • 5

2 Answers2

1

Override the onPause() method of all your activities, and turn off/pause/stop the music when it's called. You might want use a boolean flag to check if you're moving within your app, a scenario in which you'd want the music to continue:

boolean movingInApp = false;
....
movingInApp = true;
Intent intent...
.....
public void onPause() {
    if(!movingInApp) {
        //stop service
    }
}

public void onResume() {
    movingInApp = false;
    //Rest of your code
}

By setting the value of movingInApp to true before launching any intent etc, you can prevent your app from stopping the music. Remember to set it to false again later in your onResume() method. If the system makes your app go to the background, this will be false, and your music will be stopped.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • Thanks very mach! but where i need to put this code? in main.xml ? – Gil Ralph Sep 29 '12 at 11:15
  • This should be adapted to fit into all the activities it is required in. I have provided that basic skeleton code. Beyond that, it depends completely on your app design as to where it goes. – Raghav Sood Sep 29 '12 at 11:16
  • 2
    @GilRalph, how have you added sound to your app, when you even are not aware where you have to put the provided code... – azizbekian Sep 29 '12 at 11:20
  • Oh i so fail... guys i forgot to say that i convert my swf game to apk.... and the swf(flash) game is have inside the sounds :\ forgot it! – Gil Ralph Sep 29 '12 at 11:35
  • Raghav, the onPause and onResume always get called even if we are just switching between activities. How can get it to work only if the app is exited? – Ruchir Baronia Nov 11 '15 at 21:44
  • 1
    I've done a full write-up on how to implement this answer, in case others are having trouble: https://gamedevalgorithms.com/2017/06/05/selectively-playing-tracks-whilst-game-is-active-open/ – Jake Lee Jun 11 '17 at 17:57
  • Err, new link, sorry: https://blog.jakelee.co.uk/selectively-playing-tracks-whilst-game-is-active-open/ – Jake Lee Nov 29 '18 at 20:14
1

When you click back, the app keeps on running in the background. If you want to stop the music any time you get out of a certain activity override onStop or onPause to stop your sound. Something like that

@Override
protected void onStop(){
    super.onStop();
    //your code for stopping the sound
}
Jose L Ugia
  • 5,960
  • 3
  • 23
  • 26
  • i convert my swf game to apk.... and the swf(flash) game is have inside the sounds :\ forgot it! – Gil Ralph Sep 29 '12 at 11:45
  • So what you are doing is reproducing the whole swf? or re-building everything in Java? In the first case you'll have to stop the flash movie, in the second it will still work stopping the sound. – Jose L Ugia Sep 29 '12 at 12:19
  • no i just show the swf... the game is swf but in apk... all the files(audio and all the game) is in the swf...(flash) – Gil Ralph Sep 29 '12 at 12:29
  • so thanks to you! i get it now only! i need to stop\pause the flash(swf) when i click home button so how i doing this? – Gil Ralph Sep 29 '12 at 12:50
  • Override the onStop or onPause methods in your main activity (where the game is). The same way you can start playing again the swf in the onResume or onStart methods. I'd use onResume / onPause – Jose L Ugia Sep 29 '12 at 19:13
  • The onStop method always get called even if we are just switching between activities. How can get it to work only if the app is exited? – Ruchir Baronia Nov 11 '15 at 21:45
  • You can check if an activity is the last one in the stack. You could stop the music at this point. http://stackoverflow.com/questions/5975811/how-to-check-if-an-activity-is-the-last-one-in-the-activity-stack-for-an-applica – Jose L Ugia Nov 11 '15 at 21:59