音频焦点客户端死于媒体播放器在后台播放音乐

Audio focus client died with media player playing music in background

我创建了一个通过服务播放音乐的音乐播放器,但是当我离开应用程序一段时间后,音乐会随机停止,并且我正在获取日志消息 "Audio focus client died",我无法使用弄清楚..为什么会这样

我没有附加任何代码源,因为这实际上是一个简单的代码,没有执行任何复杂的任务只有一个播放音乐的服务和一个监视音频焦点的音频焦点助手..

可能是因为 Android OS 会因资源不足而终止服务。你可以让你的服务前台来避免它。这是片段:

private void sendToForeground() {
    Notification note = new Notification(0, null,
            System.currentTimeMillis());
    note.flags |= Notification.FLAG_NO_CLEAR;
    startForeground(0, note);
}

在服务的onCreate中调用即可。