Search code examples
androidandroid-activitymedia-player

pause main background music on different activity Android


so I'm developing an android game. My main activity or the intro activity has the background music of the game. Overall it is the main background music of the entire game. So I let the music keep on playing:

 private void startBgSound() {
    // TODO Auto-generated method stub
    //int soundFile = R.raw.backgroundmusic;
    //AssetFileDescriptor afd = getResources().openRawResourceFd(soundFile);

    bgsound = new MediaPlayer();
    bgsound.reset();
    bgsound = MediaPlayer.create(this, R.raw.backgroundmusic);

    bgsound.setLooping(true);
    bgsound.setVolume(100, 100);
    bgsound.start();
 } 

by setting the loop to true. When the user should click the play button (still on the main activity) the next activity sits on top of the main activity. But in my second activity, I have put there a pause button, so when the user clicks on that, the background music should also pause as well.

Do you have any clever ideas on how to do that? I'm stuck with this issue. So I'd really appreciate if you could help. Thanks.


Solution

  • In my game, I handle the music with a static class. Just give the context to it when you start playing the music and you can stop the music anywhere in your code.

    Just make sure to reset the class either when the user leaves or opens the game as static classes may keep on living even though the game has been closed.

    Here's an example:

    import android.content.Context;
    import android.media.MediaPlayer;
    
    public class SoundHandler {
        private static MediaPlayer backgroundMusic;
        private static Context context;
    
        private static boolean isMuted = false;
    
        public static void setContext(Context cont){
            context = cont;
        }
    
        public static void playMusic(int resource){
            if(backgroundMusic != null) backgroundMusic.reset();
    
            try{
                backgroundMusic = MediaPlayer.create(context, resource);
    
                backgroundMusic.setLooping(true);
                backgroundMusic.setVolume(100, 100);
    
                if(!isMuted){
                    backgroundMusic.start();
                }
            } catch (NullPointerException e){
                //Creating MediaPlayer failed. This happens randomly without any clear reasons.
                e.printStackTrace();
            }
        }
    
        public static void setMuted(boolean muted){
            if(backgroundMusic != null){
                if(muted){
                    if(backgroundMusic.isPlaying()){
                        backgroundMusic.stop();
                        isMuted = true;
                    }
                } else {
                    if(!backgroundMusic.isPlaying()){
                        backgroundMusic.start();
                        isMuted = false;
                    }
                }
            }
        }
    
        public static void quit(){
            if(backgroundMusic != null){
                backgroundMusic.release();
            }
        }
    }
    

    Remember to set the context in your onCreate() and run the quit() function in your onDestroy().