Android background music for several activities; How to catch the press of the "Home" button

I want to play an audio file in the background of my application. Simple enough. I want the music to be saved and NOT to stop and not pause when switching between actions in my application. It is also quite easy and simple to do this using the onCreate method:

    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      ...
      mp = MediaPlayer.create(MainActivity.this, R.raw.lostmexicancity);
      mp.setLooping(true);
      mp.start();
    }

This problem? Getting music to stop when I press the HOME button.

Killing the sound when the user presses the back button seems easy. Here's what works fine for me:

    public void onPause() {

      if(this.isFinishing()){  //BACK was pressed from this activity
        mp.stop();
      }

    super.onPause(); }

Not difficult, but it doesn’t catch pressing the HOME button. If you click the Home button, the music will continue to play even if the user no longer sees my application.

, , , . , . , , , "" STILL , , , , "" ( , , - , , )

, , "", - stop() onPause, , , , ,

, , , , , , , , .

Google Play , , , ? onPause(), , , , .

+4
2

Android- ( ), , (, ?)

:

, MainActivity, MainActivity Btn2, SecondActivity Btn3, ThirdActivity.

MainActivity:

public static boolean shouldPlay = false;

onStop():

public void onStop() {
    super.onStop();
    if (!shouldPlay) { // it won't pause music if shouldPlay is true
        player.pause();
        player = null;
    }
}

boolean shouldPlay true, onStop() , . , . MainActivity SecondActivity, Intent , shouldPlay true:

Button Btn2 = (Button) findViewById(R.id.Btn2);
    Btn2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, SecondActivity.class);
            shouldPlay = true;
            startActivity(intent);
        }
    });

Btn3.

, , , , MainActivity SecondActivity ThirdActivity, shouldPlay true. , , false, Second ThirdActivity ( onCreate()), , , onStop() from Main onCreate() ( , ). , , toPlay false , onCreate() Main:

shouldPlay = false;

. , , , Bro.

+1
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if(keyCode == KeyEvent.KEYCODE_HOME){
        Log.d("Jorgesys", "Home button pressed!!!");
    }
    return super.onKeyDown(keyCode, event);
}

! 4.0 +, :

"":

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK ) {
        Log.d("Jorgesys", "Back button pressed!!!");
    }
    return super.onKeyDown(keyCode, event);
}

, :

isFinishing(): , true; false, , .

public void onPause() {
      if(this.isFinishing()){  //INCORRECT, Here you are pausing you activity not finishing.
        mp.stop();
      }
    super.onPause(); 
}

:

public void onPause() {
    if(mp.isPlaying())
      {
       mp.stop();
      }
    super.onPause(); 
}

, , MediaPlayer, , .

0

Source: https://habr.com/ru/post/1525525/


All Articles