Q:

AVAudioRecorder shows time delay for recording sound in IOS 5

I am using AVAudioRecorder to record sound in 'wav' format. My code works fine for ios 4 and recording is done perfectly. But in case of ios 5, a time delay of 3-4 seconds occur in 2 out of 10 attempts for recording sound. I also used : [audioRecorder prepareToRecord] function in order to start the recording without delay.
Is there any bug in my code or somrthing else... Please guide me..
My code is:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryRecord error:nil];
NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] initWithCapacity:10];

[recordSettings setObject:[NSNumber numberWithInt: kAudioFormatLinearPCM] forKey: AVFormatIDKey];
[recordSettings setObject:[NSNumber numberWithFloat:44100.0] forKey: AVSampleRateKey];
[recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
[recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[recordSettings setObject:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
[recordSettings setObject:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];   

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *recDir = [paths objectAtIndex:0];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audio%d.wav", recDir,nextCount]];
NSError *error = nil;
audioRecorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSettings error:&error];
audioRecorder.delegate=self;

if(!audioRecorder){
  NSLog(@"recorder: %@ %d %@", [error domain], [error code], [[error userInfo]   description]);
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle: @"Warning" message: [error localizedDescription] 
delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
return;
}

[audioRecorder prepareToRecord];
audioRecorder.meteringEnabled = YES;

BOOL audioHWAvailable = audioSession.inputIsAvailable;
if (! audioHWAvailable) {
UIAlertView *cantRecordAlert =
[[UIAlertView alloc] initWithTitle: @"Warning" message: @"Audio input hardware not available" delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[cantRecordAlert show];
[cantRecordAlert release]; 
return;
}

[audioRecorder prepareToRecord];
[audioRecorder recordForDuration:(NSTimeInterval) 25];
[recordSettings release];

}

else
{
[recordButton setBackgroundImage:[UIImage imageNamed:@"record.png"] forState:UIControlStateNormal];
    self.recordBtn=NO;
    [audioRecorder stop];
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
}

A:

The problem was with AVAudioSession. Every time when i record the video, i create a session for recording:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryRecord error:nil];

and this was the problem sometimes session takes time to start and recording was delayed by 3-4sec. So, i created the session in viewdidload and make it global and the problem was resolved. Thanks to the suggestion by hotpaw2 on a similar kind of link...

