首页 > 解决方案 > “try AVAudioSession.sharedInstance().setCategory”仅在设备上返回 nil

问题描述

try AVAudioSession.sharedInstance()
                  .setCategory(AVAudioSessionCategoryPlayback,
                               with: AVAudioSessionCategoryOptions(rawValue: UInt(UInt8(AVAudioSessionCategoryOptions.defaultToSpeaker.rawValue)
                                                                                | UInt8(AVAudioSessionCategoryOptions.allowAirPlay.rawValue)
                                                                                | UInt8(AVAudioSessionCategoryOptions.allowBluetooth.rawValue)
                                                                                | UInt8(AVAudioSessionCategoryOptions.allowBluetoothA2DP.rawValue))))

返回错误:

域=NSOSStatusErrorDomain 代码=-50 "(null)"

标签: iosiphoneswift

解决方案


对于将来发现此问题的任何人来说,这就是解决方案。如果您更改AVAudioSessionCategoryPlaybackAVAudioSessionCategoryPlayAndRecord这样,它仅适用于设备:

try AVAudioSession.sharedInstance()
                  .setCategory(AVAudioSessionCategoryPlayAndRecord,
                               with: AVAudioSessionCategoryOptions(rawValue: UInt(UInt8(AVAudioSessionCategoryOptions.defaultToSpeaker.rawValue)
                                                                                | UInt8(AVAudioSessionCategoryOptions.allowAirPlay.rawValue)
                                                                                | UInt8(AVAudioSessionCategoryOptions.allowBluetooth.rawValue)
                                                                                | UInt8(AVAudioSessionCategoryOptions.allowBluetoothA2DP.rawValue))))

推荐阅读