ExoPlayer停止在后台播放

Ken*_*ndt 5 android exoplayer

我有一个应用程序,它保留ExoPlayer实例的全局实例,以便在后台运行音频流.打开大量应用后,音频停止播放.

它发生如下:

  • 打开Activity,开始播放音频
  • 按后退按钮关闭 Activity
  • 音频仍在播放,如果您单独离开设备(如预期),则会继续播放

但是,当您在最后一步之后打开十几个或更多应用程序时,ExoPlayer会在某个时刻停止播放.我的猜测是发生了内存清理,因此ExoPlayer被解除分配.我试图从日志中获取更多信息,但到目前为止这几乎没有提供任何帮助.

在内部保留ExoPlayer的参考android.app.Service并没有什么区别.

我正在测试的设备是带有Android 5.1.x的Nexus 5,但问题也发生在其他设备上.

我无法在ExoPlayer 文档页面中找到解决方案,也无法在StackOverflow或Google上找到解决方案.有谁知道防止ExoPlayer停止播放的正确方法?

Ric*_*rdo 10

为了确保Service尽可能保持活着而不被系统杀死,您需要确保将其作为前台服务启动.

这意味着将有一个通知通知用户活动服务,以便他能够了解它.因此,您必须使用相应的通知启动服务.以下是文档中的示例:

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);
Run Code Online (Sandbox Code Playgroud)

  • 对于未来的访问者:Android 8.0+ 需要频道 ID 才能收到通知。更多信息在这里。https://developer.android.com/training/notify-user/channels (2认同)