简体   繁体   中英

How to stop mediaplayer when home button is pressed

I've tried overriding onPause and everything but the mp3 file continues to play even after the home button is pressed. I have logged the onPause method and it does seem to be working, however it is not stopping the media player.

I believe that when the home button is pressed the object in which my mediaplayer is held in is set to NULL. Is there anyway to stop the music when the home button is pressed?

User870380 is correct, the home key cannot be overridden, and for good reason. The MediaPlayer is quite erratic and buggy, so seeing your exact code will be very useful. Your code will probably look something like this:

@Override
protected void onPause() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
                        }
}

You may also need to check to see if it throws an IllegalStateException. The MediaPlayer does that. A lot.

Are you getting any errors in your logcat when you try to stop the MediaPlayer?

Are you running the MediaPlayer in a service?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM