AVAudioSession + AVAudioPlayer Switch Between Internal and Loud Speaker

Below is code to switch between internal and loud speaker for AVAudioPlayer

 

void EnableSpeakerPhone ()

{

    UInt32 dataSize = sizeof(CFStringRef);

    CFStringRef currentRoute = NULL;

    OSStatus result = noErr;

    

    AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &dataSize, &currentRoute);

    

    // Set the category to use the speakers and microphone.

    UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord;

    result = AudioSessionSetProperty (

                                      kAudioSessionProperty_AudioCategory,

                                      sizeof (sessionCategory),

                                      &sessionCategory

                                      );

    

    Float64 sampleRate = 44100.0;

    dataSize = sizeof(sampleRate);

    result = AudioSessionSetProperty (

                                      kAudioSessionProperty_PreferredHardwareSampleRate,

                                      dataSize,

                                      &sampleRate

                                      );

    

    // Default to speakerphone if a headset isn’t plugged in.

    UInt32 route = kAudioSessionOverrideAudioRoute_Speaker;

    dataSize = sizeof(route);

    result = AudioSessionSetProperty (

                                      // This requires iPhone OS 3.1

                                      kAudioSessionProperty_OverrideCategoryDefaultToSpeaker,

                                      dataSize,

                                      &route

                                      );

    

    AudioSessionSetActive(YES);

}

void DisableSpeakerPhone ()

{

    UInt32 dataSize = sizeof(CFStringRef);

    CFStringRef currentRoute = NULL;

    OSStatus result = noErr;

    

    AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &dataSize, &currentRoute);

    

    // Set the category to use the speakers and microphone.

    UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord;

    result = AudioSessionSetProperty (

                                      kAudioSessionProperty_AudioCategory,

                                      sizeof (sessionCategory),

                                      &sessionCategory

                                      );

    

    Float64 sampleRate = 44100.0;

    dataSize = sizeof(sampleRate);

    result = AudioSessionSetProperty (

                                      kAudioSessionProperty_PreferredHardwareSampleRate,

                                      dataSize,

                                      &sampleRate

                                      );

    

    // Default to speakerphone if a headset isn’t plugged in.

    // Overriding the output audio route

    // The Trick is here

    

    UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_None;

    dataSize = sizeof(audioRouteOverride);

    AudioSessionSetProperty(

                            kAudioSessionProperty_OverrideAudioRoute,

                            dataSize,

                            &audioRouteOverride);

    

    

    AudioSessionSetActive(YES);

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s