Search code examples
androidmedia-playerwakelock

Using WakeLock to Keep A Stream Playing


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.


Solution

  • 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.