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.

sai
  • 548
  • 10
  • 30
  • 1
    Refer to this answer: http://stackoverflow.com/questions/9477922/android-broadcast-receiver-for-screen-on-and-screen-off – bclymer May 07 '13 at 13:22

3 Answers3

2

I have already faced with similar issue, the following code solved my problem:

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

        if (!mPowerManager.isScreenOn()) 
             if (mp!= null && mp.isPlaying())
                mp.stop();
    }
Ali
  • 2,012
  • 3
  • 25
  • 41
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/

Ali
  • 1,409
  • 13
  • 23
0

I think You need to set up your code to stop sound in receiver of Screen off

here is more detail about how to deal with screen lock receiver . http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/

dharmendra
  • 7,835
  • 5
  • 38
  • 71