0

We're developing an Android game and we play some background music for our intro (we have an Intro Activity) but we want it to continue playing to the next Activity, and perhaps be able to stop or play the music again from anywhere within the application.

What we're doing at the moment is play the bgm using MediaPlayer at our Intro Activity. However, we stop the music as soon as the user leaves that Activity. Do we have to use something like Services for this? Or is MediaPlayer/SoundPool enough? If anyone knows the answer, we'd gladly appreciate your sharing it with us. Thanks!

  • You can try a `Service` like [here](http://stackoverflow.com/questions/8209858/android-background-music-service) – Laur Ivan Aug 12 '16 at 18:44

1 Answers1

-1

The best way will be to use one single activity and fragments over it. You can write the code to start the music on onResume method of activity and stop it in onPause or in OnStop function. That way even when you change fragments, the music keeps playing and also you can handle music in a fragment by using the following like code

In any Fragment

MainActivity parentActivity = (MainActivity)getActivity();
parentActivity.startMusic();
parentActivity.stopMusic();

The startMusic and stopMusic will be public methods in the MainActivity.

Umair Farooq
  • 1,684
  • 2
  • 14
  • 25