I have a MediaPlayer in my app which starts playing on the main menu, and activity which is always open. It is created using the following code:

mp = MediaPlayer.create(MainActivity.this, R.raw.relaxedmusic);
    mp.start();
    mp.setVolume(1, 1);
    mp.setLooping(true);  

The MediaPlayer, "mp", keeps playing as new activities are opened and closed, and is muted with the following code:

public void onPause() {
    super.onPause();
    if (musicThroughActivity == 1) {
        mp.setVolume(1, 1);
    } else if (musicThroughActivity == 0) {
        mp.setVolume(0, 0);
    }

}

public void onStop() {
    super.onStop();
    if (musicThroughActivity == 1) {
        mp.setVolume(1, 1);
    } else if (musicThroughActivity == 0) {
        mp.setVolume(0, 0);
    }

}  

However, if the user exits the app from another activity (e.g., hits the home button whilts Item 1's activity is opened), the music keeps playing. Is there any way to change the volume of a MediaPlayer from an activity different to where it was created?

All help appreciated.

Related posts

Recent Viewed