Search code examples
androidmedia-playerwakelock

How to release a PARTIAL_WAKE_LOCK set through MediaPlayer?


I'm setting a PARTIAL_WAKE_LOCK for my MediaPlayer instance using:

mMediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);

How can I release it? Is it done in MediaPlayer.release() (I can't see anything about it in the doc)?


Solution

  • It is released on MediaPlayer.release(), MediaPlayer.stop(), and MediaPlayer.reset(). It's also released onCompletion and onError.

    Looking through the source for MediaPlayer here:

    http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.2.2_r1/android/media/MediaPlayer.java#MediaPlayer

    It looks like MediaPlayer.stayAwake() is responsible and this will release the wakelock.

    private void stayAwake(boolean awake) {
        if (mWakeLock != null) {
            if (awake && !mWakeLock.isHeld()) {
                mWakeLock.acquire();
            } else if (!awake && mWakeLock.isHeld()) {
                mWakeLock.release();
            }
        }
        mStayAwake = awake;
        updateSurfaceScreenOn();
    }
    

    This is a private method but it is called in various states, for example in release() stayAwake(false) is called.

    Just search through for mWakeLock and stayAwake(false) and you'll see. I've also checked as far back as 1.5 and it's the same.