gpt4 book ai didi

android - ExoPlayer 停止在后台播放

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:08:19 27 4
gpt4 key购买 nike

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

它发生如下:

  • 打开开始播放音频的Activity
  • 按下后退按钮关闭Activity
  • 如果您不理会设备(如预期的那样),音频仍在播放并继续播放

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

android.app.Service 中保留对 ExoPlayer 的引用没有任何区别。

我正在测试的设备是装有 Android 5.1.x 的 Nexus 5,但其他设备上也会出现此问题。

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

最佳答案

为了确保 Service 尽可能保持 Activity 状态而不会被系统杀死,您需要确保将其作为 foreground service 启动。 .

这意味着将有一个通知通知用户 Activity 服务,以便他可以知道。因此,您必须使用相应的通知启动服务。这是文档中的示例:

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);

关于android - ExoPlayer 停止在后台播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34400026/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com