Using WakeLock to Keep A Stream Playing

自闭症网瘾萝莉.ら 提交于 2019-11-29 04:09:50

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.

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.

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.

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!