When I set:
[[AVAudioSession sharedInstance] setCategory:
AVAudioSessionCategoryPlayAndRecord error:NULL];
…recording and playing works
I ran into this today, and most of the answers don't seem to apply to recent iOS changes. Lucian's answer above with the defaultToSpeaker option is correct, although still seems to behave differently in Simulator and on the device from time to time. What helped for me was to set category, mode and options in one call, which is available in iOS 10+ like this:
try session.setCategory(AVAudioSessionCategoryPlayAndRecord, mode: AVAudioSessionModeDefault, options: .defaultToSpeaker)
Using this audio was all the way up to maximum and stayed that way.