Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVAudioRecorder Record method returns NO at random times

I’m currently developing an app which supports in-app audio recording. The user can get a standard tableview of locally saved audio files he/she have already recorded through the app, or press a button to go to a new recording view, from where it is possible to record a new audio session, which will automatically get saved to the apps sandbox. Now this function works well most of the time. I can record new files and play them from the app, but at random times the audio recorder will fail to start a recording in this method:

-(void) startRecording
{
if (!isRecording)
{
    isRecording = YES;

    BOOL recordingSucess = [audioRecorder record];

    if (recordingSucess)
        NSLog(@"WE STARTED THE RECORDING!");
    else
        NSLog(@"WE DID NOT START THE RECORDING!");

    // Start the timer
    recordTiming = [NSDate date];

    timeCheck = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timeCheck) userInfo:nil repeats:YES]
}

That is, the NSLog will print out “We did not start the recording”, indicating that it failed to start the recording. This results in an empty audio file still being saved to the app with a constant file size of exactly 4096 bytes. This is how the audio recorder object is initialized:

-(id) initWithContentURL:(NSURL *)contentURL
{
self = [super init];

if (self)
{
    NSLog(@"Initializing audio recorder!");

    // Specific settings for the audio recording
    NSDictionary *recordSettings = [NSDictionary 
                                    dictionaryWithObjectsAndKeys:
                                    [NSNumber numberWithInt:AVAudioQualityMin],
                                    AVEncoderAudioQualityKey,
                                    [NSNumber numberWithInt:8], 
                                    AVEncoderBitRateKey,
                                    [NSNumber numberWithInt: 2], 
                                    AVNumberOfChannelsKey,
                                    [NSNumber numberWithFloat:22000.0], 
                                    AVSampleRateKey,
                                    nil];

    NSError *error = nil;

    audioRecorder = [[AVAudioRecorder alloc] initWithURL:contentURL settings:recordSettings error:&error];
    audioRecorder.delegate = self;
}

There is also NSLog checks which checks to see if the error is nil or not, so at this point I’m sure that the initialization does not seem to fail. What exactly could cause the record method to fail at seemingly random times, while it works perfectly good at most other times?

like image 360
Luffen Avatar asked Mar 14 '12 16:03

Luffen


1 Answers

Before the recording is started an audio session should be initialized:

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;
}
err = nil;
[audioSession setActive:YES error:&err];
if(err){
   NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
   return;
}

See the accepted answer for reference.

like image 86
A-Live Avatar answered Oct 07 '22 18:10

A-Live