0

I implemented a wakelock to avoid stopping my MediaPlayer (playing local files only) when screen goes off. Since I don't want to drain battery, I tried a PARTIAL_WAKE_LOCK. But it has no effect: screen off kills my player. FULL_WAKE_LOCK works fine, but screen stays on, as expected, draining the battery...

What am I doing wrong?

Thanks! L.

Luis A. Florit
  • 2,169
  • 1
  • 33
  • 58

2 Answers2

3

It looks like you are using the wake lock for the wrong purpose. From what I understood based on your explanation: you need the playback to continue when the screen goes off:

To do that - I would recommend you try the following logic.

  • Try to have the player running in a service (not in the activity)
  • Start the service with startForeground method (this will need you to include a notification as well)
  • Use a Messenger to communicate between your activity and service.

But the wake lock may come handy; as this may be helpful in handling a audio stutter issue in future. I am not very sure about this part as I myself is yet to try out the wake lock as a solution to stuttering issue.

(I am not a professional/commercial programmer ; rather an ad-hoc developer who finds a requirement designs a solution, develops it and uses it for myself. So my solution might not be a 100% professional approach but I am sure it does the job)

Good Luck and happy coding - S.Mani

S.Mani
  • 31
  • 2
  • Yes, I'm trying to implement the player as a service now. Harder than I thought, but good for learning. Still, I don't understand why a partial wakelock does not work... Thanks! – Luis A. Florit Jan 10 '13 at 11:59
  • The partial wake lock is best suited for a service. To my knowledge an Activity is not capable of holding a partial wake lock, and you certainly don't want to keep the screen on for an audio app, just to keep a wake lock. It's unclear whether the startForeground is necessary or not, and if you did use startForeground, why would you need a wake lock at all? – iHearGeoff Jun 03 '13 at 23:17
-1

Wake Lock is to make sure the device does NOT go into standby.

What you want is to listen for Intent.ACTION_SCREEN_OFF that tells you that the screen is now off

NikkyD
  • 2,209
  • 1
  • 16
  • 31
  • Not really (maybe I wasn't clear enough?). I precisely want to prevent the device to go into standby, in order for the music to continue playing. – Luis A. Florit Oct 28 '12 at 15:27
  • you should check ur onPause and onStop, because thats the only thing that happens if you hold a partial wake lock and someone hits the power button – NikkyD Oct 28 '12 at 15:49
  • I don't understand. I only want to prevent the device to standby, but not the screen. I want the music to continue, but the screen to go off after the usual timeout (I don't really care about the powerbutton, that would be fine). I don't want to arrive to a onPause method, because I don't want the phone to pause at all. That's a wakelock for (as you said). In other words: why the phone is going to stanby even with a PARTIAL_WAKE_LOCK? According to http://developer.android.com/reference/android/os/PowerManager.html this shouldn't happen, no? – Luis A. Florit Oct 28 '12 at 17:37