AVAudioSession recording making other sounds quiet – #in

Introduction

I’m making an app which both needs to play ‘tap’ generated system sounds and to record user inputted sound to trigger an event. The sounds, which are triggered by the UITapGestureRecognizer play SystemSounds via the SoundEffect Class, whilst the recorder uses the AVAudioSession Class.

The audio was working fine in the simulator, but when played on my iPhone all sound was diverted to the receiver ( that’s the small telephone speaker at the top rather than the larger speaker by the dock-connector), which is much quieter.

Source of the Solution

The solution was found here: problem with AVAudioSession and Voice Recording

“When you start recording on an iPhone the audio will switch from the main speaker to the in call speaker which has lower volume, you need to redirect the audio route. “( From Steve Oldmeadow)

The Code

The Audio Session Programming Guide in the iOS developer documentation lists the solution:

“..Changing the default output route. … This option is available starting in iOS 3.1…

I’ll paste the code snippet here:

---
---
// need to import now
#import <AudioToolbox/AudioToolbox.h>

---
---
-(void) initializeRecorder{
 //Instanciate an instance of the AVAudioSession object.
 AVAudioSession * audioSession = [AVAudioSession sharedInstance];
 //Setup the audioSession for playback and record.
 [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: &error];
 //Activate the session
 [audioSession setActive:YES error: &error];
 
 // change the default output
 UInt32 doChangeDefaultRoute = 1;
 
 AudioSessionSetProperty (
 
  kAudioSessionProperty_OverrideCategoryDefaultToSpeaker,
 
  sizeof (doChangeDefaultRoute),
 
  &doChangeDefaultRoute
 
  );
}

Conclusion

Hopefully this post will be good for my own future refrence and perhaps to others.

 

 

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