Search code examples
iosobjective-caudiopcmrecording

How do you record a PCM audio file on iOS?


Is it possible to record a PCM audio file through the microphone? I am trying to record a very short PCM audio file but have struggled so far.

I have tried using an AVAudioRecorder but this doesn't work:

AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryRecord error:nil];
NSDictionary* recorderSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                  [NSNumber numberWithInt:kAudioFormatLinearPCM],AVFormatIDKey,
                                  [NSNumber numberWithInt:44100],AVSampleRateKey,
                                  [NSNumber numberWithInt:1],AVNumberOfChannelsKey,
                                  [NSNumber numberWithInt:16],AVLinearPCMBitDepthKey,
                                  [NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey,
                                  [NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,
                                  nil];
NSError* error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSURL* outURL = [NSURL fileURLWithPath:[documentsDirectory stringByAppendingPathComponent:@"output.pcm"]];
recorder = [[AVAudioRecorder alloc] initWithURL:outURL settings:recorderSettings error:&error];
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
[recorder record];

It would be much appreciated if someone could point me in the right direction, thank you.


Solution

  • Do you know about Audio Queue Services? If you know, you can easily. In others, you can do it using AVAudioRecord. Please check the below URL: How do I record audio on iPhone with AVAudioRecorder? Thank you.