On each release of iOS, more of the audioSession properties are migrated to AVFoundation, so you should use those in preference whenever available.
Since iOS 6 kAudioSessionProperty_OverrideAudioRoute
is represented in AVAudioSession by the method
- (BOOL) overrideOutputAudioPort:error:
Available values are AVAudioSessionPortOverrideNone
and AVAudioSessionPortOverrideSpeaker
Here is an example audio session configured entirely via AVFoundation:
- (void) configureAVAudioSession
{
//get your app's audioSession singleton object
AVAudioSession* session = [AVAudioSession sharedInstance];
//error handling
BOOL success;
NSError* error;
//set the audioSession category.
//Needs to be Record or PlayAndRecord to use audioRouteOverride:
success = [session setCategory:AVAudioSessionCategoryPlayAndRecord
error:&error];
if (!success) NSLog(@"AVAudioSession error setting category:%@",error);
//set the audioSession override
success = [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker
error:&error];
if (!success) NSLog(@"AVAudioSession error overrideOutputAudioPort:%@",error);
//activate the audio session
success = [session setActive:YES error:&error];
if (!success) NSLog(@"AVAudioSession error activating: %@",error);
else NSLog(@"audioSession active");
}
UPDATE
Since iOS 7.0, the Audio Session Services C API is now fully deprecated in favour of AVAudioSession.
转载:http://stackoverflow.com/questions/18807157/how-do-i-route-audio-to-speaker-without-using-audiosessionsetproperty