We have developed an iOS app using WebRTC for video communication. We have the option to toggle between headphone and speaker.
We are facing an app crash when bluetooth headphone is connected and the user tries to toggle between speaker and headphone continuously.
audioSession.lockForConfiguration()
var bluetoothPort : AVAudioSessionPortDescription? = nil
var headphonePort : AVAudioSessionPortDescription? = nil
var inbuiltMicPort : AVAudioSessionPortDescription? = nil
if let availableInputs = AVAudioSession.sharedInstance().availableInputs {
for inputDevice in availableInputs {
if inputDevice.portType == .builtInMic {
inbuiltMicPort = inputDevice
} else if inputDevice.portType == .headsetMic || inputDevice.portType == .headphones {
headphonePort = inputDevice
} else if inputDevice.portType == .bluetoothHFP || inputDevice.portType == .bluetoothA2DP || inputDevice.portType == .bluetoothLE {
bluetoothPort = inputDevice
}
}
}
do {
try audioSession.overrideOutputAudioPort(AVAudioSession.PortOverride.none)
if let bluetoothPort = bluetoothPort {
try audioSession.setPreferredInput(bluetoothPort)
try audioSession.setMode(AVAudioSession.Mode.voiceChat.rawValue)
audioSession.unlockForConfiguration()
return true
} else if let headphonePort = headphonePort {
try audioSession.setPreferredInput(headphonePort)
try audioSession.setMode(AVAudioSession.Mode.voiceChat.rawValue)
audioSession.unlockForConfiguration()
return true
} else if let inbuiltMicPort = inbuiltMicPort {
try audioSession.setMode(AVAudioSession.Mode.default.rawValue)
try audioSession.setPreferredInput(inbuiltMicPort)
audioSession.unlockForConfiguration()
return true
} else {
audioSession.unlockForConfiguration()
return false
}
} catch let error as NSError {
debugPrint("#configureAudioSessionToSpeaker Error \(error.localizedDescription)")
audioSession.unlockForConfiguration()
return false
}
}
func speakerOn() -> Bool {
audioSession.lockForConfiguration()
var inbuiltSpeakerPort : AVAudioSessionPortDescription? = nil
if let availableInputs = AVAudioSession.sharedInstance().availableInputs {
for inputDevice in availableInputs {
if inputDevice.portType == .builtInSpeaker {
inbuiltSpeakerPort = inputDevice
}
}
}
do { ///Audio Session: Set on Speaker
if let inbuiltSpeakerPort = inbuiltSpeakerPort {
try audioSession.setPreferredInput(inbuiltSpeakerPort)
}
try audioSession.setMode(AVAudioSession.Mode.videoChat.rawValue)
try audioSession.overrideOutputAudioPort(.speaker)
audioSession.unlockForConfiguration()
return true
} catch let error as NSError {
debugPrint("#configureAudioSessionToSpeaker Error \(error.localizedDescription)")
audioSession.unlockForConfiguration()
return false
}
}
Any help will be appreciated to solve this crash issue.