Using WakeLock to Keep A Stream Playing
Asked Answered
T

6

11

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.

Travis answered 21/6, 2011 at 23:26 Comment(0)
S
11

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.

Spring answered 1/7, 2011 at 0:40 Comment(1)
So there is a chance for the mediaplayer to suddenly stop playing? Or is this just an extra precaution? And what's the difference if MediaPlayer acquires the lock, or I do?Travis
S
15
  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.

Sighted answered 27/6, 2011 at 20:10 Comment(5)
But maybe different phones act differently? Also, does the emulator emulate this as well?Travis
Hm. Good point. Different phones might act differently but as far as I know this "two minute" behaviour is standard with Android. If someone can give you an exception then take their word over mine. Nevertheless, better safe than sorry right? It's not hard to acquire a wake lock. Why not grab one to be sure that your application will always work as expected?Sighted
so acquiring a wake lock where it's not needed won't have ant "side-effects"?Travis
I've worked with an application where several wake locks were acquired at once and there were no negative side effects (there were independent threads which needed wake locks without having knowledge of one another). Other than the obvious battery drain (which will occur anyway if the MediaPlayer instance already has a WakeLock) you shouldn't see any negative impacts on your program if you acquire one unnecessarily.Sighted
It is definitely okay to acquire multiple wake locks. The meaning of a wake lock is "while holding it the CPU stays awake." If you are holding two wake locks, you just have two things wanting the CPU to stay awake and as you would expect it stays awake.Spring
S
11

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.

Spring answered 1/7, 2011 at 0:40 Comment(1)
So there is a chance for the mediaplayer to suddenly stop playing? Or is this just an extra precaution? And what's the difference if MediaPlayer acquires the lock, or I do?Travis
G
4

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.

Glycerin answered 1/7, 2011 at 15:34 Comment(0)
B
1

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.

Bona answered 22/6, 2011 at 1:2 Comment(0)
M
0
  mediaPlayer.setScreenOnWhilePlaying(true);

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

Mihalco answered 27/8, 2018 at 7:37 Comment(0)
M
-1

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

Milne answered 1/7, 2011 at 20:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.