2

I saw lots of similar questions, but I couldn't figure out the best way in my case. I want to play some background music in my app (looped). It should start (if a sharedPreference is set) at the start of the app. If the user disable a checkbox, it should stop (if he enabled it, it should start again, preferences is set here...).

Nearly everything worked fine by using an MediaPlayer. But I don't know, how to stop the music if the user tabs the home button. Because of playing music across Activities, I think it doesn't seem wise to stop in onPause() or onDestroy()?!

I read a lot about AsyncTask and Services, because I know it's not good to play the music in the MainThread instead of an WorkerThread. But I couldn't figure out the best solution and how to handle it.

Is there a way to play music until I say explicitly "stop" (in any Activity) or the user tabs the home screen button without editing each Activity?

lis
  • 670
  • 1
  • 13
  • 32
  • You can try to achieve that with a boradcast receiver http://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html – GhostDerfel Jan 28 '14 at 17:33
  • when using the `MediaPlayer` or a Service? What's the Event for "App isn't in the foreground anymore" and "now, it's back in the foreground"? – lis Jan 28 '14 at 18:35

0 Answers0