AVAudioSession setCategory总是返回错误

Pra*_*ati 3 ios avaudiosession swift

我正在开发音乐应用程序,该应用程序仅播放应用程序中的歌曲。现在我正在连接蓝牙扬声器播放。所以我设置AVAudioSession类别,但它总是会返回错误。

代码:

 func setupSessionForPlaying() {
    let audioSession = AVAudioSession.sharedInstance()
    do {
        try audioSession.setCategory(AVAudioSessionCategoryPlayback, with: [.allowBluetooth])
        try audioSession.setActive(true)
    } catch {
        fatalError("Error Setting Up Audio Session")
    }
}
Run Code Online (Sandbox Code Playgroud)

我在调用这个函数didFinishLaunchingWithOptionsAppDelegate

但是,如果我更改setCategoryaudioSession.setCategory(AVAudioSessionCategoryPlayback)正常工作。

有人知道这段代码有什么问题吗?

谢谢

Pra*_*iya 5

根据文档 https://developer.apple.com/documentation/avfoundation/avaudiosession/categoryoptions/1616518-allowbluetooth

仅当音频会话类别为playAndRecord或record时,才可以设置此选项。

allowBluetooth您不能使用 AVAudioSessionCategoryPlayback

希望对您有所帮助