[英]AVAudioRecorder shows time delay for recording sound in IOS 5
I am using AVAudioRecorder to record sound in 'wav' format. 我正在使用AVAudioRecorder以'wav'格式录制声音。 My code works fine for ios 4 and recording is done perfectly.
我的代码适用于ios 4,录制完美。 But in case of ios 5, a time delay of 3-4 seconds occur in 2 out of 10 attempts for recording sound.
但是对于ios 5,录制声音的10次尝试中有2次会出现3-4秒的时间延迟。 I also used : [audioRecorder prepareToRecord] function in order to start the recording without delay.
我还使用了:[audioRecorder prepareToRecord]功能,以便立即开始录制。
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];
}
The problem was with AVAudioSession. 问题出在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. 这是问题,有时会话需要时间开始,录制延迟3-4秒。 So, i created the session in viewdidload and make it global and the problem was resolved.
所以,我在viewdidload中创建了会话并使其全局化并解决了问题。 Thanks to the suggestion by hotpaw2 on a similar kind of link...
感谢hotpaw2对类似链接的建议......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.