Thursday, June 6, 2013

AVAudioSession AVAudioSessionCategoryPlayAndRecord example in Objective C (iOS).


AVAudioSession AVAudioSessionCategoryPlayAndRecord

NSString *const AVAudioSessionCategoryPlayAndRecord;

Allows recording (input) and playback (output) of audio, such as for a VOIP (voice over IP) app.
Your audio continues with the Silent switch set to silent and with the screen locked. (The switch is called the Ring/Silent switch on iPhone.) To continue playing audio when your app transitions to the background (for example, when the screen locks), you need to add the audio value to the “UIBackgroundModes” in Information Property List Key Reference key in your information property list file.(AVAudioSessionCategoryPlayAndRecord)
This category is appropriate for simultaneous recording and playback, and also for apps that record and play back but not simultaneously. If you want to ensure that sounds such as Messages alerts do not play while your app is recording, use the AVAudioSessionCategoryRecord category instead.
This category normally prevents audio from other apps from mixing with your app's audio. To allow mixing when using this category, use the kAudioSessionProperty_OverrideCategoryMixWithOthers property.

AVAudioSession AVAudioSessionCategoryPlayAndRecord example.
[[AVAudioSession sharedInstance] setCategory:
    AVAudioSessionCategoryPlayAndRecord error:NULL];
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute,
    sizeof(audioRouteOverride), &audioRouteOverride);

Example of [AVAudioSession AVAudioSessionCategoryPlayAndRecord].
NSError *setCategoryErr = nil;
NSError *activationErr  = nil;
//Set the general audio session category
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: &setCategoryErr];

//Make the default sound route for the session be to use the speaker
UInt32 doChangeDefaultRoute = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof (doChangeDefaultRoute), &doChangeDefaultRoute);

//Activate the customized audio session
[[AVAudioSession sharedInstance] setActive: YES error: &activationErr];

AVAudioSession AVAudioSessionCategoryPlayAndRecord example.
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:NULL];

    UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
    AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);

    NSURL* soundUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"HsTR_soundEFX_2-1" ofType:@"mp3"]];
   audioAlert= [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
    audioAlert.delegate = self;
    [audioAlert prepareToPlay];
    float maxVolume = 1.0;
    float currentVolume=[MPMusicPlayerController applicationMusicPlayer].volume;
    [[MPMusicPlayerController applicationMusicPlayer] setVolume:maxVolume];
    [audioAlert play];
   //set up timer to wait until audioAlert is finished playing,
   // and then call the line below to set the volume back to it's original setting
    [[MPMusicPlayerController applicationMusicPlayer]setVolume:currentVolume];

End of AVAudioSession AVAudioSessionCategoryPlayAndRecord example article.