I am creating a CallKit app that only does outbound calls. We are doing our own interface, not using the built in UI. I need to put a speaker button on the screen that will switch from speaker/no-speaker mode.
I have followed Apple's guidelines and example CallKit code and it appears to be working well enough.
I could not find much information on how to switch to and from speaker mode. I am using the following function to change the audio route. Does anyone have experience doing this? Will this work reliably?
func speaker(on: Bool) {
let audioSession = AVAudioSession.sharedInstance()
do {
// not sure I need this ...
try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
if on {
// does this trigger a change to the audio route?
try audioSession.overrideOutputAudioPort(.speaker)
} else {
// is this how to switch back to the phone speaker?
try audioSession.overrideOutputAudioPort(.none)
}
} catch {
// Audio session change failure
print("failed to change speaker phone")
}
}