Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Audio Recording File Format

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];
     }
like image 590
user1673099 Avatar asked Sep 15 '12 08:09

user1673099


People also ask

What format are voice recordings in?

There are three major groups of audio file formats: Uncompressed audio formats, such as WAV, AIFF, AU or raw header-less PCM; Formats with lossless compression, such as FLAC, Monkey's Audio (filename extension . ape ), WavPack (filename extension .

Does iPhone record in M4A?

Voice Memos, the stock sound recorder app on iPhone, allows users to capture any sound, voice or audio and save them as . m4a files. You can't use it to record audio in other formats on iPhone, like WAV, MP3. If you need to record audio in MP3 or WAV format on iPhone, you need a third-party sound recorder app.

Can iPhone record WAV files?

To record a WAV file on iPhone, you need a good WAV recorder app to back you up with a WAV file format. Hokusai Audio Recorder is a beneficial, easy-to-use, and flexible app to create WAV files for you on your iPhone. It can save WAV or M4A audio files and convert M4A or other audio files to WAV format.

How do I convert iPhone recording to MP3?

Touch the Edit icon at the right corner to open the Edit screen. Tap the Edit icon again to pop up the File Format menu. You can find WAV, M4A, M4R and MP3 from the menu list. Select MP3 or WAV to convert the voice memo from M4A to MP3 or WAV.


1 Answers

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.

like image 58
Scott Miller Avatar answered Sep 22 '22 05:09

Scott Miller