Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone SDK AVAudioRecorder delay before recording

I'm using the AVAudioRecorder to grab audio from the microphone. The problem i'm facing is this: the first time I start recording there is a 2 second delay between when I tell the recorder to start and when it actually starts. Subsequent recording calls execute much quicker. Are there any obvious bugs here? What can I do to speed up 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 start the recorder by calling my startRecoring method:

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

Here is the Log output and you can see that there is roughly 2.5 seconds between the two NSLog calls. On the first time clicked but not other times:

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
like image 275
slayton Avatar asked Apr 13 '11 19:04

slayton


1 Answers

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

The preparation invoked by this method takes place automatically when you call record. Use prepareToRecord when you want recording to start as quickly as possible upon calling record.

try calling prepareToRecord after initializing the audio recorder

like image 105
Zaky German Avatar answered Nov 07 '22 15:11

Zaky German