Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

swift

iphone

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

like image 248
Zack117 Avatar asked Jan 27 '23 11:01

Zack117


1 Answers

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))))
like image 52
Zack117 Avatar answered Feb 13 '23 06:02

Zack117