iOS 13在后台关闭应用程序:音频流问题

7

我在使用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()

苹果每年喜欢进行更改。目前有人在iOS 13和Xcode 11 / Swift 5上做到了这一点吗?

谢谢!

更新:我刚刚发现这个问题仅影响通过WebViews播放的音频。我不确定这是否是iOS 13上的错误,还是苹果确实从多任务处理中删除了WebViews。因此,我更改了我的应用程序,现在通过AVPlayer播放音频。背景音频再次正常工作。


分享你的与AVAudioSession相关的代码片段。 - Syed Sadrul Ullah Sahad
现在先使用AVPlayer。AVaudioPlayer对我也不起作用。 - Celeste
1个回答

0

你在Appdelegate中放置了恢复播放音频的代码吗?

func applicationDidEnterBackground(_ application: UIApplication) {


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

}

在你的目标功能中添加音频、AirPlay和画中画


这并不会阻止audioPlayer在后台停止,OP可以使用AVPlayer代替,我的AVPlayerController可以让我的音乐在后台播放,而audioPlayer则会在几秒钟后关闭。 - Celeste
@K4747Z 你是如何解决这个问题的? - tomDev
为什么要用 try?这不是安卓啊...或者说,随着 Tim Cook 的加入,它变成了安卓... - Nikita

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接