You can do this in iOS 5 with properties:
kAudioSessionProperty_InputSource kAudioSessionProperty_OutputDestination
For possible values ββ(which sources / destinations are available on the device), use AudioSessionGetProperty with properties:
kAudioSessionProperty_InputSources kAudioSessionProperty_OutputDestinations
For iOS 3.1+, I assume that you are using the PlayAndRecord audio session - you can try setting kAudioSessionProperty_OverrideCategoryDefaultToSpeaker to true. For instance,
UInt32 defaultToSpeaker = 1; status = AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof (defaultToSpeaker), &defaultToSpeaker );
It may not be exactly what you are looking for, but I donβt think you can get closer in <iOS 5.
source share