Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 328
yydl Avatar asked Jun 21 '11 23:06

yydl


People also ask

What does it mean when an app holds a Wakelock?

A wakelock, in layman's terms, is just a way for an app to keep the CPU/screen/other things awake when the phone is idle in order to perform a specific background task.

How do you use Wakelock?

To release the wake lock, call wakelock. release() . This releases your claim to the CPU. It's important to release a wake lock as soon as your app is finished using it to avoid draining the battery.

Which of the following keeps the screen turned on while watching movies and playing games Android application?

Open Settings. Tap Display. Tap Sleep or Screen timeout. Select how long you want your Android smartphone or tablet screen to stay on before turning off due to inactivity.

What is a partial Wakelock?

Partial wake locks are a mechanism in the PowerManager API that lets developers keep the CPU running after a device's display turns off (whether due to system timeout or the user pressing the power button). Your app acquires a partial wake lock by calling acquire() with the PARTIAL_WAKE_LOCK flag.


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

like image 98
hackbod Avatar answered Sep 22 '22 00:09

hackbod


  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.

like image 25
Dylan Knowles Avatar answered Sep 19 '22 00:09

Dylan Knowles