1

Hi I need to stop sound from mediaplayer on screen Lock As I am using

if(mp!= null && mp.isPlaying()){
    mp.stop();
  }

in onPause() . But there is no result. So, How can I stop my media player sound after screen is locked.

To say exactly, I have 16 mp3 files, which will come in random.if a sound is playing and the screen is locked, present sound is Stopped and next one sound is playing.After screen is unlocked, it will works as proper & when the screen is locked , Again same is repeating on my Android 2.3.6 version mobile. How to overcome this.

Thank you.

4

3 回答 3

2

我已经遇到过类似的问题,下面的代码解决了我的问题:

    @Override
    public void onPause()
    {
        super.onPause();
        PowerManager mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);

        if (!mPowerManager.isScreenOn()) 
             if (mp!= null && mp.isPlaying())
                mp.stop();
    }
于 2013-05-07T13:25:21.130 回答
1

You would need to handle it via Screen On & Off intents.

See the link below for details:
http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/

于 2013-05-07T13:22:30.957 回答
0

我认为您需要设置代码以停止屏幕关闭接收器中的声音

这是有关如何处理屏幕锁定接收器的更多详细信息。http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/

于 2013-05-07T13:28:35.563 回答