使用 WakeLock 保持流播放

发布于 2024-11-16 10:52:31 字数 237 浏览 1 评论 0原文

我有一个在服务中运行的 MediaPlayer,该服务正在播放来自 URL(流媒体)的音频。到目前为止,它似乎运行良好,甚至当我将手机置于待机状态时还能继续播放。

我目前没有获取唤醒锁。我的问题是:

  • 在我的情况下实际上有必要获取唤醒锁吗?
  • 如果有必要,我应该获取什么类型的唤醒锁?

是的,这是唤醒锁的合法用例,因为我的用户明确希望音频继续播放。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一直在等你来 2024-11-23 10:52:31
  1. 手机待机时观看约五分钟。如果继续播放,则不需要唤醒锁;它可能表明 MediaPlayer 实例已经有一个。在 Android 中,用户不活动大约两分钟后,任何没有唤醒锁的非必要操作都将被挂起;五分钟应该可以消除对两分钟计时器的任何疑问。
  2. 尝试部分唤醒锁定。它可以让您的用户在处理器时听到音频将保持“清醒”。但是,由于允许屏幕进入睡眠状态,因此在显示图像时不会浪费电池。这可能就是您想要的。

编辑:如果您想安全起见,那么您需要使用 WakeLock。这样,如果 MediaPlayer 发生更改并允许在手机挂起时进入睡眠状态,您的程序仍将正常工作。只要您在不再需要时正确释放它,添加 WakeLock 确实不会有任何损失。如果不这样做,您将消耗比您预期更多的电池,在最坏的情况下,您将立即看到一条错误,指示您在应用程序终止时没有释放锁。添加 WakeLock(虽然可能是多余的)是一种很好的做法,因为它使您的应用程序能够更稳健地应对其所依赖的软件的更改。

  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.

征﹌骨岁月お 2024-11-23 10:52:31

默认情况下,MediaPlayer 不会自动为您执行此操作。

但是,您不必获取唤醒锁,而是可以调用一种方法来告诉它在玩游戏时为您保留一个唤醒锁:

http://developer.android.com/reference/android/media/MediaPlayer.html#setWakeMode(android.content.Context , int)

请注意,正如文档所述,它仍然是您的应用程序持有唤醒锁,因此要使用此功能,您需要请求唤醒锁权限。

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.

游魂 2024-11-23 10:52:31

您可能需要 WakeLock,因为您无法保证 PowerManager 在播放期间不会启动并休眠。 PARTIAL_WAKE_LOCK 将确保使用最低水平的电池消耗(CPU 打开;屏幕/键盘关闭)。您始终可以测试电池消耗的影响,但我怀疑它会很大,因为无论如何 CPU 都必须打开才能播放音乐。此方法将确保无论使用什么手机(或该手机上的设置),播放都不会因 CPU 进入睡眠状态而中断。

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.

回眸一遍 2024-11-23 10:52:31

我认为您不需要为此使用 WakeLock。当第一次开始使用 MediaPlayer 时,我很快发现它在待机状态下不会关闭。我花了一些工作来克服这个问题,但我从未见过待机导致流媒体 MediaPlayer 对象死亡的情况。

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.

愿与i 2024-11-23 10:52:31
  mediaPlayer.setScreenOnWhilePlaying(true);

文档说:
“在可能的情况下,这是优于‘setWakeMode’的首选方法,因为它不需要应用程序具有低级唤醒锁访问权限。”

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

像极了他 2024-11-23 10:52:31

您不需要唤醒锁。如果您使用 WAKE LOCK,您将强制用户保持屏幕打开,我个人更喜欢在播放媒体时关闭屏幕。

这里是长时间运行服务的示例示例

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文