Home » Android » android – Using WakeLock to Keep A Stream Playing

android – Using WakeLock to Keep A Stream Playing

Posted by: admin May 14, 2020 Leave a comment


I have a MediaPlayer running in a Service that’s playing audio from a URL (streaming). As of now it appears to work well, and even continues playing when I put the phone in standby.

I currently do not acquire a wakelock. My question is:

  • Is it actually necessary to acquire a wakelock in my situation?
  • If it is necessary, what type of wakelock should I acquire?

And yes, this is a legitimate use-case for wakelock, because my users explicitly want the audio to continue playing.

How to&Answers:

MediaPlayer does not do this for you automatically by default.

However, instead of you having to acquire a wake lock, it has a method you can call to tell it to hold one for you while playing:

http://developer.android.com/reference/android/media/MediaPlayer.html#setWakeMode(android.content.Context, int)

Note that, as the documentation says, it is still your app holding the wake lock so to use this function you will need to request the wake lock permission.


  1. Watch the phone for about five minutes when it is on standby. If it continues playing, you do not need a wake lock; it probably indicates that the MediaPlayer instance already has one. In Android, after about two minutes of inactivity from the user anything non-essential which is without a wakelock will be suspended; five minutes should remove any doubt surrounding the two minute timer.
  2. Try a partial wake lock. It’ll let your users hear the audio as the processor will be kept “awake.” However, it won’t waste battery on displaying an image as the screen is allowed to go to sleep. This is probably what you want.

EDIT: If you want to play on the safe side then you want to use a WakeLock. That way if MediaPlayer ever changes and is allowed to go to sleep when the phone suspends your program will still work correctly. There really is nothing to lose by adding the WakeLock providing that you corretly release it when it is no longer required. If you do not you will simply drain more battery than you intend to and, in the worst case, you will immediately see an error indicating that you did not release the lock when your application terminates. Adding a WakeLock – while potentially redundant – is a good practice as it makes your applicaiton more robust against changes to the software that it depends upon.


You probably will need a WakeLock, since you cannot guarantee that the PowerManager won’t kick in and sleep during playback. The PARTIAL_WAKE_LOCK will ensure that the lowest level of battery drain is employed (CPU on; screen/keyboard off). You can always test the effect of the battery drain but I doubt it will be large, since the CPU must be on anyway in order to play the music. This method will ensure that no matter what phone is used (or settings on said phone), the playback won’t be cut from the CPU going to sleep.


I don’t think you need a WakeLock for this. When first starting out with MediaPlayer, I found out very quickly that it just won’t shut up in standby. It took me a little bit of work just to overcome that, but I’ve never seen a case where standby causes a streaming MediaPlayer object to die.



The documentation says:
“This is the preferred method over ‘setWakeMode’ where possible, since it doesn’t require that the application have permission for low-level wake lock access.”


You don’t require a WAKE LOCK. If you use WAKE LOCK, you will force users to keep their screen on, I personally prefer to turn the screen off while playing media.

Example of long running service hereexample