简体   繁体   English

如何在按下主页按钮时停止Media Player

[英]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. 我尝试覆盖onPause,但即使按了home键,但mp3文件仍可以继续播放。 I have logged the onPause method and it does seem to be working, however it is not stopping the media player. 我已经记录了onPause方法,它似乎可以正常工作,但是并没有停止媒体播放器。

I believe that when the home button is pressed the object in which my mediaplayer is held in is set to NULL. 我相信当按下主页按钮时,我的媒体播放器所在的对象将设置为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. User870380是正确的,不能覆盖Home键,这是有充分理由的。 The MediaPlayer is quite erratic and buggy, so seeing your exact code will be very useful. MediaPlayer非常不稳定且容易出错,因此查看确切的代码将非常有用。 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. 您可能还需要检查它是否抛出IllegalStateException。 The MediaPlayer does that. MediaPlayer做到了。 A lot. 很多。

Are you getting any errors in your logcat when you try to stop the MediaPlayer? 尝试停止MediaPlayer时,您的logcat是否出现任何错误?

Are you running the MediaPlayer in a service? 您是否在服务中运行MediaPlayer?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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