iPhone – Configuring the AVAudioSession and Output Audio to iPhone Speaker

I am working on the AVAudioRecorder and AVAudioPlayer. By default, the output audio is routed to the receiver instead of the iPhone speaker. In order to fulfill my requirement, i have to configure the AVAudioSession in the AppDelegate.

There are 2 ways to route the audio output to speaker.

  • Overriding the output audio route
  • Changing the default output audio route (iPhone OS 3.1 or above)


If you only overriding the output audio route, the audio output will revert to the receiver instead of the speaker when u plugging in and then unplugging the headset. Therefore, it’d be better to change the default output audio route. but please note that it only support iPhone OS 3.1 or above.

Here comes to the codes.
1. Load the AVFoundation and AVToolbox frameworks into your Xcode project.

2. Import the header file in the AppDelegate.h

#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>

 

3. Add the following code in applicationDidFinishLaunching method in AppDelegate.m

	// Set AudioSession
	NSError *sessionError = nil;
	[[AVAudioSession sharedInstance] setDelegate:self];
	[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];

	/* Pick any one of them */
 	// 1. Overriding the output audio route
	//UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
	//AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride);

	// 2. Changing the default output audio route
	UInt32 doChangeDefaultRoute = 1;
	AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof(doChangeDefaultRoute), &doChangeDefaultRoute);

 

Then u can try the playback with the AVAudioPlayer
 

Done =)

Reference: Audio Session Cookbook – Redirecting Output Audio

18 thoughts on “iPhone – Configuring the AVAudioSession and Output Audio to iPhone Speaker”

  1. no.no. I meant as in a switch to make the sound come out to one speaker – the phone speaker or the receiver speaker.

    Like

    1. i haven’t tried this before.

      1. create a switching function in the appDelegate which take a bool as input parameter and change the audio route.

      2. then in your view, add a button and implement the click action which called the appDelegate to change the route.

      i am not sure if it works or not since i dun know if the route can be changed on the fly.

      Let me know what you find. =P

      Like

      1. o dear! Unfortunately, it doesn’t work. Here’s an example of what i want though: If you go to the Appstore and search for the free app and download it: “QuickVoice Recorder”, there is a bar at the top of the view, which has the button “Speaker”.
        That speaker button enables you to switch the iphone speaker and the receiver.
        That’s something that i want to recreate, (& i can’t find it anywhere!). Any help is much appreciated.

        Like

      2. The iphone posts in this blog were all written when i was working in my last job. personally i dun have a mac machine with xcode installed.

        Try to search some examples on the web and take a look on the iphone OS api reference. good luck =)

        Like

      3. Can you put this comment into a bit more detail. I may have been doing something wrong, though i’m not entirely sure. Thanks.

        Like

  2. 1. create a switching function in the appDelegate which take a bool as input parameter and change the audio route.
    2. then in your view, add a button and implement the click action which called the appDelegate to change the route.

    please give more detail…

    Like

    1. have you tried to follow the tutorial to create a simple app?
      XCode iPhone SDK Tutorial – Playing a Sound with AVAudioPlayer

      if you can complete it, then try to follow the code in this post and override the audio route.

      after you have completed the above tasks. then you could try to create a button on your view and see if you can override the audio route by clicking the button instead of override it in the applicationDidFinishLaunching method.

      Like

  3. I’ve already created an app that plays sounds using the uipickerview, just that i want to give users the option of switching between the main iphone speakers and the receiver. Have you got a project file demonstrating that?

    Like

  4. Hey Hello ykyuen!
    This code is not working for me ! :((((
    I implement this in app didFinishLaunchingWithOptions then simply play this file
    NSBundle* bundle = [NSBundle mainBundle];
    NSString *topClickFile = [bundle pathForResource:@”music”ofType:@”mp3″];
    sprayAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:topClickFile] error:NULL];
    [sprayAudio play];

    PROBLEM is when i plug a headphone into my iPod it does not play from speaker. :((
    please reply, waiting for reply.
    vyasvandana11@gmail.com.

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.