I am implementing the Audio recording. It works fine with caf
& wave
format. But the problem is file size is too big.
So, can anyone h
I also was attempting to use AVAudioRecorder to record audio in an AAC encoded format preferably in a .m4a file. I was quickly able to get the code to record to AAC inside of a core audio file (.caf), but I couldn't get AVAudioRecorder to properly format a .m4a. I was just about to rewrite the recording code in my application using the lower level Audio Units API but instead I put that on the back burner and added in the code to handle the shared audio session. Once that was set up, I went back and tried to save as an .m4a and it just worked.
Here is some example code:
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
NSURL *tmpFileUrl = [NSURL fileURLWithPath:[docsDir stringByAppendingPathComponent:@"tmp.m4a"]];
NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey,
[NSNumber numberWithFloat:16000.0], AVSampleRateKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
nil];
NSError *error = nil;
AVAudioRecorder *recorder = [[AVAudioRecorder alloc] initWithURL:tmpFileUrl settings:recordSettings error:&error];
[recorder prepareToRecord];
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryRecord error:nil];
[session setActive:YES error:nil];
[recorder record];
Then to end the recording I used:
[recorder stop];
AVAudioSession *session = [AVAudioSession sharedInstance];
int flags = AVAudioSessionSetActiveFlags_NotifyOthersOnDeactivation;
[session setActive:NO withFlags:flags error:nil];
Then the file at 'tmpFileUrl' can be used as you please.
Here are my settings for high quality and small file size (on balance):
NSDictionary *recordSettings = @{AVEncoderAudioQualityKey: @(AVAudioQualityMedium),
AVFormatIDKey: @(kAudioFormatMPEG4AAC),
AVEncoderBitRateKey: @(128000),
AVNumberOfChannelsKey: @(1),
AVSampleRateKey: @(44100)};
This gives you a close to cd quality mono AAC track. You should append the file with .m4a to be readable everywhere. All other parameters get set to device defaults, which is what you want to happen in most cases.
NSMutableDictionary *settings = [[NSMutableDictionary alloc] initWithCapacity:0];
[settings setValue :[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
[settings setValue:[NSNumber numberWithFloat:8000.0] forKey:AVSampleRateKey];
[settings setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
[settings setValue :[NSNumber numberWithInt:8] forKey:AVLinearPCMBitDepthKey];
[settings setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
[settings setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];
//Encoder
[settings setValue :[NSNumber numberWithInt:12000] forKey:AVEncoderBitRateKey];
[settings setValue :[NSNumber numberWithInt:8] forKey:AVEncoderBitDepthHintKey];
[settings setValue :[NSNumber numberWithInt:8] forKey:AVEncoderBitRatePerChannelKey];
[settings setValue :AVAudioQualityMin forKey:AVEncoderAudioQualityKey];