简体   繁体   中英

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?

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.

Is contentURL always the same or do you generate a new one each time?

Also could you try explicitly calling prepareToRecord before record and checking whether it succeeds or not?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM