【问题标题】:ExoPlayer stops playing in backgroundExoPlayer 停止在后台播放
【发布时间】:2015-12-21 16:18:06
【问题描述】:

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

它发生如下:

  • 打开开始播放音频的Activity
  • 按返回键关闭Activity
  • 如果您不理会设备(如预期),音频仍在播放并继续播放

但是,当您在最后一步之后打开十几个或更多应用程序时,ExoPlayer 会在某个时候停止播放。 我的猜测是发生了内存清理,因此 ExoPlayer 被释放。我试图从日志中获取更多信息,但到目前为止帮助不大。

android.app.Service 中保留 ExoPlayer 的引用并没有什么不同。

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

我在 ExoPlayer documentation 页面以及 StackOverflow 或 Google 上都找不到解决方案。有谁知道防止 ExoPlayer 停止播放的正确方法?

【问题讨论】:

  • 你试过把它放在前台Service吗?
  • 是的,我尝试在android.app.Service 中保留对它的引用。
  • 我想知道如果它是从该服务中创建的是否会有所不同...
  • 我不是指任何服务,而是从foreground 开始的服务。
  • 谢谢,我试试看!

标签: android exoplayer


【解决方案1】:

要确保Service 尽可能保持活动状态而不会被系统杀死,您需要确保以foreground 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);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多