When I set:
[[AVAudioSession sharedInstance] setCategory:
AVAudioSessionCategoryPlayAndRecord error:NULL];
…recording and playing works
I found out (cf. Listing 7-9 in the iOS documentation) that the suggested solution above on overriding the audio route stops working after plugging, and removing earphones.
So, if you want the change in audio route to be permanent in the current audio session (Listing 7-10 in the iOS documentation) the same source you can set the default audio route by instead using
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
//Set the general audio session category
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: &setCategoryErr];
//Make the default sound route for the session be to use the speaker
UInt32 doChangeDefaultRoute = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof (doChangeDefaultRoute), &doChangeDefaultRoute);
//Activate the customized audio session
[[AVAudioSession sharedInstance] setActive: YES error: &activationErr];
and of course make sure to link the AudioToolbox framework and import it using
#import