Using WakeLock to Keep A Stream Playing

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.


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.