【问题标题】:iOS 13 Closing App on Background: Audio Stream IssueiOS 13 在后台关闭应用程序:音频流问题
【发布时间】:2020-01-25 04:04:30
【问题描述】:

我一直在使用 Swift 5 和 Xcode 11 在后台播放音频。

启用后台模式和启动 AVAudioSession 不再起作用,并且当您离开应用程序几秒钟后应用程序继续关闭。

首先我得到了一个权利预热,但流开始了:

Error acquiring assertion: <NSError: 0x2829830f0; domain: RBSAssertionErrorDomain; code: 2; reason: "Required client entitlement is missing"> {
userInfo = {
    RBSAssertionAttribute = <RBSLegacyAttribute: 0x1050a9040; requestedReason: MediaPlayback; reason: MediaPlayback; flags: PreventTaskSuspend | PreventTaskThrottleDown | WantsForegroundResourcePriority>;
}

在后台几秒钟后关闭:

[ProcessSuspension] Background task expired while holding WebKit ProcessAssertion (isMainThread? 1).

如果我让应用程序保持打开状态,音频仍然会停止:

[ProcessSuspension] 0x10dfc8848 - ProcessAssertion::processAssertionWasInvalidated()

Apple 每年都喜欢改变这一点。到目前为止,有人在 iOS 13 和 Xcode 11/Swift 5 上运行了吗?

谢谢!

更新:我刚刚发现这个问题只影响通过 WebView 播放的音频。我不确定这是否是 iOS 13 上的错误,或者 Apple 是否真的从多任务处理中删除了 WebView。所以我改变了我的应用程序,现在正在通过 AVPlayer 播放音频。背景音频再次起作用。

【问题讨论】:

  • 分享你的AVAudioSession相关代码sn-p
  • 现在使用 AVPlayer。 AVaudioPlayer 也不适合我

标签: ios swift audio background audio-streaming


【解决方案1】:

您是否在 Appdelegate 中添加了恢复播放音频的代码

func applicationDidEnterBackground(_ application: UIApplication) {


    do {
        try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [.mixWithOthers, .allowAirPlay])
        try AVAudioSession.sharedInstance().setActive(true)
    } catch {
        print(error)
    }

}

从您的目标功能中添加音频、播放和画中画

【讨论】:

  • 这也不会阻止 audioPlayer 在后台停止,OP 可以使用 AVPlayer 代替,我的 AVPlayerController 将我的音乐保留在后台,而 audioPlayer 在几秒钟后关闭。
  • @K4747Z 你究竟是如何解决这个问题的?
  • 为什么try会这样???这不是 android... 或者实际上与 tim cook 一起变成了 android...
猜你喜欢
  • 2019-12-22
  • 2014-06-11
  • 1970-01-01
  • 1970-01-01
  • 2019-08-29
  • 1970-01-01
  • 1970-01-01
  • 2021-01-05
  • 1970-01-01
相关资源
最近更新 更多