Delay iPhone SDK AVAudioRecorder before recording

I use AVAudioRecorder to capture microphone sound. The problem I am facing is this: the first time I start recording, there is a 2 second delay between when I say that the recording device starts and when it really starts. Subsequent recording calls are much faster. Are there any obvious errors here? What can I do to speed up the time it takes to start the initial recording.

I initialized the recorder in viewDidLoad:

- (void)viewDidLoad { NSString *fileName = @"test.aiff"; [super viewDidLoad]; NSString *docsDir = [NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *soundFile = [docsDir stringByAppendingPathComponent:fileName]; NSURL *soundFileURL = [NSURL fileURLWithPath:soundFile]; NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:AVAudioQualityMin], AVEncoderAudioQualityKey, [NSNumber numberWithInt:16], AVEncoderBitRateKey, [NSNumber numberWithInt: 2], AVNumberOfChannelsKey, [NSNumber numberWithFloat:44100.0], AVSampleRateKey, nil]; NSError *error = nil; audioRecorder = [[AVAudioRecorder alloc] initWithURL:soundFileURL settings:recordSettings error:&error]; } 

Then, when I'm ready, I started the recorder by calling the startRecoring method:

 -(void) startRecording{ NSLog(@"Trying to start Recording"); [audioRecorder record]; NSLog(@"Recording started"); } 

Here is the output of the log, and you can see that there is approximately 2.5 seconds between two calls to NSLog. First time click, but not another time:

 First recorder call 2011-04-13 15:41:47.495 AudioRecorderTest[6570:207] Trying to start Recording 2011-04-13 15:41:49.869 AudioRecorderTest[6570:207] Recording started Next Recorder Call 2011-04-13 15:42:49.236 AudioRecorderTest[6570:207] Trying to start Recording 2011-04-13 15:42:49.246 AudioRecorderTest[6570:207] Recording started 
+6
source share
2 answers

Creates an audio file at the location specified by the url parameter in initWithURL: settings: error: method. If the file already has a location, this method overwrites it.

The drug caused by this method occurs automatically when you record a call. Use prepareToRecord when you want the recording to start as fast as possible after calling the record.

try calling prepareToRecord after initializing the audio recorder

+4
source

After initializing the recorder, in addition to prepareToRecord, try inserting:

 AVAudioSession *audioSession = [AVAudioSession sharedInstance]; NSError *err = nil; [audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err]; if(err) { NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]); return; } [audioSession setActive:YES error:&err]; if(err){ NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]); return; } UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord; AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory); 
0
source

Source: https://habr.com/ru/post/885855/


All Articles