I'm using MPVolumeView class for switch between audio outputs. In iPhone, you have 2 options on start (iPhone and speaker) and in iPad you have only 1 option (speaker)
But when you connect headphones on iPad there aren't speaker options only headphones.
(In iPhone all work well)
Set MPVolumeView:
private func setUI(){
let myVolumeView = MPVolumeView(frame: self.speakerButton.bounds)
myVolumeView.showsVolumeSlider = false
myVolumeView.setRouteButtonImage(nil, for: .normal)
self.speakerButton.addSubview(myVolumeView)
}
Build session:
try session.setPreferredSampleRate(Double(DefaultAudioDevice.kSampleRate))
try session.setPreferredIOBufferDuration(0.01)
let audioOptions = AVAudioSessionCategoryOptions.allowBluetooth.rawValue
try session.setCategory(AVAudioSessionCategoryPlayAndRecord, mode: AVAudioSessionModeVoiceChat, options: AVAudioSessionCategoryOptions(rawValue: audioOptions))
try session.setActive(true)
try session.overrideOutputAudioPort(AVAudioSessionPortOverride.speaker)
I try: - Add AVAudioSessionCategoryOptions.defaultToSpeaker - Change to AVAudioSessionCategoryPlayAndRecord - Remove session.overrideOutputAudioPort(AVAudioSessionPortOverride.speaker)

Try this code