Zack117
Zack117

Reputation: 1075

"try AVAudioSession.sharedInstance().setCategory" returns nil on only device

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))))

Returns the error:

Domain=NSOSStatusErrorDomain Code=-50 "(null)"

Upvotes: 5

Views: 2262

Answers (1)

Zack117
Zack117

Reputation: 1075

For anyone who finds this in the future here is the solve. It only works on device if you change AVAudioSessionCategoryPlayback to AVAudioSessionCategoryPlayAndRecord like this:

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))))

Upvotes: 8

Related Questions