ExoPlayer停止在后台播放 [英] ExoPlayer stops playing in background
问题描述
我有一个应用程序,该应用程序保留ExoPlayer实例的全局实例,以方便在后台进行音频流. 打开许多应用程序后,音频停止播放.
I have an application that keeps a global instance of an ExoPlayer instance to facilitate audio streams in the background. After opening lots of apps, the audio stops playing.
它发生如下:
- 打开
Activity
开始播放音频 - 按下后退按钮以关闭
Activity
- 如果您不理会设备,音频仍会继续播放,并且会继续播放
- open
Activity
that starts playing audio - press back button to close
Activity
- the audio is still playing and keeps doing so if you leave the device alone (as intended)
但是,当您在最后一步之后打开十几个或更多应用程序时,ExoPlayer有时会停止播放. 我的猜测是发生了内存清理,因此ExoPlayer被释放了.我试图从日志中获取更多信息,但是到目前为止,它提供的帮助很少.
However, when you open a dozen or more apps after the last step, the ExoPlayer stops playing at some point. My guess is that a memory cleanup happens and thus ExoPlayer gets deallocated. I tried to get more information from the logs, but that has provided little help so far.
将ExoPlayer的引用保留在android.app.Service
内没有任何作用.
Keeping a reference of the ExoPlayer inside an android.app.Service
doesn't make a difference.
我正在测试的设备是具有Android 5.1.x的Nexus 5,但该问题也发生在其他设备上.
The device I am testing on is a Nexus 5 with Android 5.1.x, but the issue happens on other devices too.
我无法在ExoPlayer 文档页上或在StackOverflow上找不到解决方案谷歌.有人知道防止ExoPlayer停止播放的正确方法吗?
I couldn't find a solution in the ExoPlayer documentation pages nor on StackOverflow or Google. Does anyone know the correct way to prevent the ExoPlayer from stopping playback?
推荐答案
要确保Service
尽可能有效地存活而不被系统杀死,您需要确保以前景服务.
To make sure a Service
stays alive as much as possible without being killed by the system, you need to make sure you start it as a foreground service.
这意味着将有一条通知通知用户正在使用的服务,以便他可以知道它.因此,您必须以相应的通知启动服务.这是文档中的示例:
This means there will be a notification informing the user of the active service so he can be aware of it. Because of that, you must start the service with a corresponding notification. Here is the example from the docs:
Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text),
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, ExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, getText(R.string.notification_title),
getText(R.string.notification_message), pendingIntent);
startForeground(ONGOING_NOTIFICATION_ID, notification);
这篇关于ExoPlayer停止在后台播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!