Here is what I want to achieve in my app: I have 3 menus in the startup page: PLAY , INSTRUCTIONS , HIGH SCORE. I have theme music playing in the background with the help of MediaPlayer object. I just want that the theme music should be playing for the PLAY and INSTRUCTIONS menu, uninterrupted, i.e, if the activity changes from MAIN MENU to INSTRUCTIONS and vice-versa , the music should be playing uninterrupted. But the music should stop as soon as the PLAY activity is started. I don't know the best way to do it, but all I can think of is sending the MediaPlayer object that started the theme music to PLAY's oncreate and stop it there. is there any way to do this?
Asked
Active
Viewed 930 times
0

VdesmedT
- 9,037
- 3
- 34
- 50

Aman Grover
- 1,621
- 1
- 21
- 41
-
Hi, its better to use Singleton for mediaplayer and that way you get access to this class from every activity. look here: http://stackoverflow.com/questions/12585720/how-to-use-the-singleton-pattern-in-an-android-project – SacreDeveloper Apr 28 '14 at 07:57
1 Answers
2
You want to use a Service to play the music (and to do so off of the main thread). Here are relevant pages on

Karakuri
- 38,365
- 12
- 84
- 104
-
If I start my service in Main Menu activity , I will need to stop that service in Play activity, so , I will need the service object declared and initialized in Main Menu in the Play activity. Same problem again. Or is there another way? – Aman Grover Apr 28 '14 at 08:10
-
You don't declare and initialize instances of Services, just like you don't do that for Activities (and other application components). Android does that for you. Just use the `startService` and `stopService` methods in your activities. If the service is already started, then the next startService is directed to the already running service. – Karakuri Apr 28 '14 at 08:15
-
Thank you, It looks satisfactory , I think I need to read those pages before I can comment anything further. – Aman Grover Apr 28 '14 at 08:17