I'm working on a game with a MainActivity
which opens activities with the game levels. Each of the level activities inherits a BaseActivity
.
MainActivity
never finishes. All the levels open on top of it.
I want to start my music in the MainActivity
and keep it playing throughout the rest of the application.
The catch is that I want the Service
to pause the music when the app goes to the background.
I've created a MusicService
which plays the music through the MediaPlayer
like so -
@Override
public void onCreate() {
super.onCreate();
player = MediaPlayer.create(this, R.raw.song);
player.setLooping(true);
player.setVolume(100,100);
player.start();
}
I'm confused about where to pause the music. The Service
is started from the MainActivity
but I want to be able to also pause it even when the app is paused while on any Level activity. Can I communicate with the Service
through any Activity
?
I looked at this question but I don't want my music to stop and then restart every time I destroy and create a Level activity.