0

I'm having trouble adding background music to my app. I have three activities and I would like the music to keep playing regardless of whether the user switches activities or not. I am currently using a service to play the music but this results in the music starting and stopping every time a different activity starts.

I have found this solution several times throughout forums but it doesn't seem work:

Context context = getApplicationContext();
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
    if (!taskInfo.isEmpty()) {
      ComponentName topActivity = taskInfo.get(0).topActivity; 
      if (!topActivity.getPackageName().equals(context.getPackageName())) {
        player.stop();
      }
    }

You're supposed to add it to every activities onPause, and I did that and it still didn't work. The music doesn't stop when you switch between activities, but it also doesn't stop when you exit the app.

Thanks in advance for the help.

Filipe
  • 137
  • 3
  • 12

1 Answers1

0

How is the service setup? Are you binding the service to an activity or are you passing an intent to the service. If the service is bound to an activity, that would explain why when you navigate to a different activity, that the service dies. The activity that the service is bound to is no longer there.

If you are running and passing an intent to the service, then hopefully, you aren't passing anything to the service to stop playback. If you navigate outside the app, the service should still be running in the foreground with a notification on your notification bar.

http://developer.android.com/guide/topics/media/mediaplayer.html

You might want to take a look at the guide I linked above.

midnightstar
  • 572
  • 5
  • 13
  • Ok, so I changed it up a bit and managed to get music playing throughout the app, however it doesn't stop when I exit it using the home or back button only when I close it by accessing the window where you see all the running apps. How can I fix this? – Filipe Dec 28 '14 at 15:21
  • Update, if I press back then the music stops, but if I press home then it doesn't. – Filipe Dec 28 '14 at 15:50
  • If a service is playing the music in your app, then you have to tell the servie to stop playing the music. It doesn't (and shouldn't) do it on it's own unless you code the logic for it. That's why when you hit the home or back button, the music doesn't stop playing. If you wanted the app to stop playing music when you hit the home button, you code that in. If you want the app to stop only when you hit the pause button, you also have to code that in. The app only does what you programmed it to do. – midnightstar Dec 28 '14 at 17:32
  • I know that, but my problem is that everything I have tried to do so far hasn't worked and I cannot understand why. I have overriden onStop, onPause and onDestroy and it didn't work, the music still continued to play. Any suggestions on what else I should try? – Filipe Dec 28 '14 at 18:48
  • For the home button, [override it](http://stackoverflow.com/questions/5547818/can-i-override-the-home-button-in-my-application). For the back button, I would investigate trying to see if you can determine whether or not anything is in the back stack of the application. If the back stack is empty, then next you hit back, you want the music to stop playing, because then you will be outside the app. – midnightstar Dec 28 '14 at 19:04
  • Or you can override the back button so that when you hit back on certain screens, the music also stops. – midnightstar Dec 28 '14 at 19:15