I have an app which does listen and play sound at the same time. By default, the sound output goes through the earphone. So I use the following code to route it through the spea
With AudioSessionSetProperty deprecated since iOS 7 we should be using AVFoundation AVAudioSession. Since the desired action is to allow a user action to override the route through the speaker you might consider the difference between AVAudioSessionPortOverrideSpeaker and AVAudioSessionCategoryOptionDefaultToSpeaker.
According to Technical bulletin Q&A QA1754: "When using AVAudioSessionCategoryOptionDefaultToSpeaker, user gestures will be honored. For example, plugging in a headset will cause the route to change to headset mic/headphones and unplugging the headset will cause the route to change to built-in mic/speaker".
Note that the technical bulletin explains that AVAudioSessionPortOverrideSpeaker would be more appropriate for use with a speakerphone button for example, which is not what the original post was asking for.
https://developer.apple.com/library/ios/qa/qa1754/_index.html
My own implementation is called before I invoke the player as follows:
NSError *error;
AVAudioSession* audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:&error];
// handle any error
// initiate the player or recorder
[_player play];
Also this question is similar to one addressed in a different but related post.
According to the same technical bulletin referred to here "Think of using overrideOutputAudioPort: in terms of what you might use to implement a Speakerphone button where you want to be able to toggle between the speaker (AVAudioSessionPortOverrideSpeaker) and the normal output route (AVAudioSessionPortOverrideNone)."
Refer to that post if you are looking for implementing the speaker override overrideOutputPort category: How Do I Route Audio to Speaker without using AudioSessionSetProperty?