1

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??

Community
  • 1
  • 1
Araib karim
  • 421
  • 4
  • 16

2 Answers2

0

I think this code will work for you. Add this class (Enclosed in your activity class).

public class BackgroundMusic extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        MediaPlayer backgroundmusic = MediaPlayer.create(YourActivity.this, R.raw.yourbackgroundmusic); 
        player.setVolume(100,100); 
        player.setLooping(true); 
        player.start(); 

        return null;
    }

}

And create it

BackgroundMusic bm = new BackgroundMusic();

On onResume method:

public void onResume() {
    super.onResume();
    bm.execute(null);
}

And onPause method:

public void onPause() {
    super.onPause();
    bm.cancel(true);
}

Hope this help!

AndroidLTG
  • 91
  • 1
  • 11
-1
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);
}

}

Araib karim
  • 421
  • 4
  • 16
  • You really should not use this code as it is, I really encorage anybody reading this to have a look at what audio focus is (https://developer.android.com/guide/topics/media-apps/audio-focus). Also MediaPlayer should be created in a background thread unless you use 'prepareAsync()'. – Pdroid Apr 05 '19 at 13:32