Search code examples
androidmedia-playerandroid-lifecycle

Playing Background Music in Android in a Simple Game


I am trying to play background music in simple game on Android using Services.

Using the link: Android Life Cycles

But this code doesn't work properly, onResumeActivity, onPauseActivity are called but the music keep running in background even when the onPauseActivity method is called.

The music keeps on playing while the app is in background.

is there any other way to play background music in an Android App/Game??


Solution

  • public class SoundGameBaseActivity extends Activity {
    public static boolean isSoundPaused = false;
    
    
    
    
    
    public static MediaPlayer mp;
    
    protected static final String TAG = SoundGameBaseActivity.class.getName();
    
    protected void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
    
    
        playMusic();
    }
    
    public static boolean isAppWentToBg = false;
    
    public static boolean isWindowFocused = false;
    
    public static boolean isMenuOpened = false;
    
    public static boolean isBackPressed = false;
    
    @Override
    protected void onStart() {
        Log.d(TAG, "onStart isAppWentToBg " + isAppWentToBg);
    
        applicationWillEnterForeground();
    
        super.onStart();
    }
    
    private void applicationWillEnterForeground() {
        if (isAppWentToBg) {
            //Google Analytics
            MyApp.getInstance().trackScreenView("ApplicationActivated");
            isAppWentToBg = false;
        //  Toast.makeText(getApplicationContext(), "App is in foreground",
        //          Toast.LENGTH_SHORT).show();
            playMusic();
        }
    }
    
    @Override
    protected void onStop() {
        super.onStop();
    
        Log.d(TAG, "onStop ");
    
    
            applicationdidenterbackground();
    
    
    }
    
    public void applicationdidenterbackground() {
    
        if (!isWindowFocused) {
    
            isAppWentToBg = true;
            Toast.makeText(getApplicationContext(),
                    "App is Going to Background", Toast.LENGTH_SHORT).show();
    
            stopMusic();
    
        }
    }
    
    public void TurnOnMusicAgain() {
    
    }
    
    void playMusic() {
    
    
    
    
    
            if (mp == null) {
                mp = MediaPlayer.create(this, R.raw.background1);
                // mp.prepare();
                mp.start();
                mp.setLooping(true);
    
            } else {
                if (mp.isPlaying()) {
    
                } else {
                    Log.e("", "coming back");
                    mp.start();
                }
            }
    
    }
    
    void stopMusic() {
        if (mp != null)
    
            mp.pause();
    }
    
    @Override
    public void onBackPressed() {
    
        if (this instanceof StartScreen) {
    
        } else {
            isBackPressed = true;
        }
    
        Log.d(TAG,
                "onBackPressed " + isBackPressed + ""
                        + this.getLocalClassName());
        super.onBackPressed();
    }
    
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
    
        isWindowFocused = hasFocus;
        Log.e("Is Window Focus", "" + isWindowFocused);
        if (isBackPressed && !hasFocus) {
            isBackPressed = false;
            isWindowFocused = true;
        }
    
        super.onWindowFocusChanged(hasFocus);
    }
    

    }