My app is getting crashed when App is in backGround and i received webrtc call from Notification.
func appClient(_ client: ARDAppClient!, didChange state: RTCIceConnectionState) {
switch state {
case .connected:
let audioSession = AVAudioSession.sharedInstance()
if (delegate != nil){
delegate?.appClientStatus!(client, status: .connected)
}
captureController?.startCapture()
do {
try audioSession.overrideOutputAudioPort(.speaker) // App is crashing here
} catch _ {
}
break
case .disconnected:
if (delegate != nil){
delegate?.appClientStatus!(client, status: ARDAppClientState.disconnected)
}
print("disconnected ICE")
break
default:
break
}
}
I've tried different ways and patches
Main thread
DispatchQueue.main.async { do{ try audioSession.setCategory(.playAndRecord) try audioSession.overrideOutputAudioPort(AVAudioSession.PortOverride.speaker) }catch let error{ print(error.localizedDescription) } }
setActive
do { try audioSession.setActive(true) } catch _ { } do { try audioSession.setCategory(.playAndRecord) } catch _ { } do { try audioSession.overrideOutputAudioPort(.speaker) } catch _ { }
From below reference -
AVAudioSession - How to switch between speaker and headphones output
Force audio file playback through iPhone loud speaker using Swift
But still app is getting crashed and throwing below error:-
- error
failure in assertBarrierOnQueue of FBSSerialQueue:0x28294f100 (FBSSerialQueue.m:121) : threading violation: expected the main thread