thekevinscott
thekevinscott

Reputation: 5413

4 second lag in AVAudioRecorder record

I'm using AVAudioRecorder to record audio but I'm experiencing a 4 second delay between button press and beginning to record.

Here's my setup code:

NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:

                                    [NSNumber numberWithFloat: 16000.0],AVSampleRateKey,
                                    [NSNumber numberWithInt: kAudioFormatAppleIMA4],AVFormatIDKey,
                                    [NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
                                    [NSNumber numberWithInt: AVAudioQualityMax],AVEncoderAudioQualityKey,nil];

    NSError *error = nil;

    audioRecorder = [[AVAudioRecorder alloc]
                     initWithURL:soundFileURL
                     settings:recordSettings
                     error:&error];
    if (error)
    {
        NSLog(@"error: %@", [error localizedDescription]);

    } else {
        NSLog(@"prepare to record");
        [audioRecorder prepareToRecord];
    }

-(void)record {

NSLog(@"Record");

//[audioRecorder prepareToRecord];

if (!audioRecorder.recording)

{

    NSLog(@"Record 2");

    [audioRecorder record];

    NSLog(@"Record 3");        

} 

}

Record is the function called on button press. I know prepareToRecord is called implicitly via 'record' but I wanted to see if it would affect the delay at all. It does not.

Here's the console log:

2011-10-18 21:48:06.508 [2949:707] Record
2011-10-18 21:48:06.509 [2949:707] Record 2
2011-10-18 21:48:10.047 [2949:707] Record 3

There's about 3.5 seconds before it starts recording.

Are these settings too much for the iPhone? (iPhone 4). Am I initializing it wrong?

Upvotes: 3

Views: 1121

Answers (3)

cthomaschase
cthomaschase

Reputation: 108

I'm seeing the same issue on the iOS5/iPhone4 combo. iOS4 and iPhone4S are fine. There seems to be an issue when using the new OS on older hardware. I've tried many combinations of settings as well.

Try setting the AVAudioSession category when you initialize the Audio Recorder:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

Upvotes: 1

William Entriken
William Entriken

Reputation: 39273

I had the same problem. Also, since I was switching between Playback and Record mode, this was causing long delays constantly. But when I switched to Play and Record it refused to use the loudspeaker on iPhone, it would only use the headset speaker.

Solution:

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

Upvotes: 0

eugen
eugen

Reputation: 85

I had the same issue on iPod touch 4th generation (iOS4, iOS5 and JailBreak - I used more devices) and on different devices it runs different, but I think @Charles Chase is right, it does depend of device's iOS you are testing on. Your recordSettings dictionary is ok, code also looks right except one thing, you need to add this:

audioRecorder.delegate = self;

right after you alloc and init your recorder:

    audioRecorder = [[AVAudioRecorder alloc]
                             initWithURL:soundFileURL
                             settings:recordSettings
                             error:&error];

    audioRecorder.delegate = self;

Upvotes: 0

Related Questions