13

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 help me for record audio with format which also played in window & file size is little low.

The code I tried is below:

 dirPaths = NSSearchPathForDirectoriesInDomains(
                                                    NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = [dirPaths objectAtIndex:0];
     NSString *soundFilePath = [docsDir
                                stringByAppendingPathComponent:@"sound.wave"];
     NSLog(@"%@",soundFilePath);
     NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

     NSDictionary *recordSettings = [NSDictionary 
                         dictionaryWithObjectsAndKeys:
                                    [NSNumber numberWithInt:AVAudioQualityMin],
                                     AVEncoderAudioQualityKey,
                                     [NSNumber numberWithInt:16], 
                                     AVEncoderBitRateKey,
                                     [NSNumber numberWithInt: 2], 
                                     AVNumberOfChannelsKey,
                                     [NSNumber numberWithFloat:8000.0], AVSampleRateKey,
                                     [NSNumber numberWithInt:8], AVLinearPCMBitDepthKey,
                                     nil];

     NSError *error = nil;

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

     if (error)
     {
         NSLog(@"error: %@", [error localizedDescription]);

     } else {
        [audioRecorder prepareToRecord];
     }
User97693321
  • 3,336
  • 7
  • 45
  • 69
user1673099
  • 3,293
  • 7
  • 26
  • 57
  • duplicate of http://stackoverflow.com/questions/4279311/how-to-record-voice-in-m4a-format? – nneonneo Sep 15 '12 at 08:20
  • Record in m4a (MPEG-4 AAC) format. See http://stackoverflow.com/questions/4279311/how-to-record-voice-in-m4a-format for details. – nneonneo Sep 15 '12 at 08:20

3 Answers3

16

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.

Scott Miller
  • 686
  • 6
  • 8
  • `setActive: withFlags: error:` is deprecated after iOS 6. Instead we can use `setActive: error:` (without seting flags). It works fine for me. – Vineeth Dec 10 '13 at 08:40
  • Changing from kAudioFormatMPEG4AAC to kAudioFormatMPEG4AAC, changes file size from 1.3MB to 45kb – Thiru Apr 16 '15 at 11:32
  • Has anyone tried this with iOS10? The recording is completely distorted if there is any appreciable level. – Aron Nelson Oct 24 '16 at 04:28
8

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.

Matjan
  • 3,591
  • 1
  • 33
  • 31
  • Again, this used to work fine - but is now distorted with iOS10 if there is any appreciable level. Using iPad Pro – Aron Nelson Oct 24 '16 at 04:29
1
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];
Jerry Thomsan
  • 1,409
  • 11
  • 9