Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVAudioRecorder - Proper MPEG4 AAC Recording Settings

I've got a live app with an estimated 15% of users reporting that the record feature is not working. This isn't happening on our test devices, but the reports show that the problem is that prepareToRecord is returning NO. I've had trouble finding sample settings for AAC format. Are any of my settings off? App requires iOS5 and uses ARC.

 AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryRecord error:nil];

NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
 [NSNumber numberWithInt:kAudioFormatMPEG4AAC], AVFormatIDKey,
 [NSNumber numberWithFloat:44100.0], AVSampleRateKey,
 [NSNumber numberWithInt:1], AVNumberOfChannelsKey,
 [NSNumber numberWithInt:AVAudioQualityHigh], AVSampleRateConverterAudioQualityKey,
 [NSNumber numberWithInt:128000], AVEncoderBitRateKey,
 [NSNumber numberWithInt:16], AVEncoderBitDepthHintKey,
 nil];

NSString *fileName = [NSString stringWithFormat:@"%@%@.caf", verseGUID, bRecordingReference ? @"_ref" : @""];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", [[Utilities sharedInstance] documentsDirectoryPath], fileName]];
NSError *error = nil;
audioRecorder = [[AVAudioRecorder alloc] initWithURL:url settings:recordSettings error:&error];
if([audioRecorder prepareToRecord]){
    [audioRecorder record];
}else{
    int errorCode = CFSwapInt32HostToBig([error code]);
    NSLog(@"Error: %@ [%4.4s])", [error localizedDescription], (char*)&errorCode);
}  
like image 203
bgolson Avatar asked Jul 05 '12 15:07

bgolson


3 Answers

it could be many things not having to do with the recording settings.

the real question you want answered seems to be: what would cause the recording not to occur?

audioRecorder could be nil or audioRecorder prepareToPlay could be returning NO. the former seems more likely.

the url passed to initWithURL could be malformed: - have you tested by playing with the verseGUID, bRecordReference values? maybe your devices never have a bad verseGUID, but the devices on which no recording happens have a nil/empty verseGUID. this could cause the filename to be simply ".caf".

you seem to have your own class method [Utilities sharedInstance] . could this work for some reason on your devices but not on the failing devices? if so, you could be asking to record in a top level directory when you did not mean to.

can you get the testers you have onto a "beta" list? sign up for something like TestFlight or Hockey Kit, get one or more of the users with a failure to record to also sign up, and then upload a beta of your app with diagnostics that put a dialog on screen with the resultant "error". that might be most obvious. i use testflightapp.com only because it was the first i tried, it was pretty easy for me to manage and pretty painless for my beta testers.

like image 101
john.k.doe Avatar answered Oct 01 '22 20:10

john.k.doe


Try this settings which i used to record MPEG 4 AAC format...Its working good..

NSString *tempDir = NSTemporaryDirectory();
NSString *soundFilePath = [tempDir stringByAppendingPathComponent:@"sound.m4a"];

NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                  [NSNumber numberWithInt:kAudioFormatMPEG4AAC], AVFormatIDKey,
                                  [NSNumber numberWithInt:AVAudioQualityMin], AVEncoderAudioQualityKey,
                                  [NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
                                  [NSNumber numberWithFloat:8000.0], AVSampleRateKey,
                                  nil];
like image 2
Dinesh Raja Avatar answered Oct 04 '22 20:10

Dinesh Raja


Apologies that this is slightly tangential but I've just had a very similar issue with a number of users not being able to record, which I thought was related to audio settings.

However for me it turned out that they had denied permission to access the microphone. This meant that prepareToRecord was working (and truncating the file) and record was reporting that it was working, but actually it wasn't recording anything.

And I used this when I want to record:

AVAudioSession.sharedInstance().requestRecordPermission({(granted: Bool)-> Void in
    if granted {
        // can record - create AVAudioRecorder here

    } else {
        // can't record - update UI (or react accordingly)
    }
})

Hope it saves someone some time if they run into a similar issue.

like image 1
atomoil Avatar answered Oct 05 '22 20:10

atomoil