Saving a recorded AVAudioRecorder sound file: Now what? ( iOS, Xcode 4 )

13,191

Voice Record Demo is a great example to take a look at. It uses Cocos2D for the UI but if you just take a look at the HelloWorldScene.m class, it contains all the code you need to:

  1. Create a new audio session
  2. Check if the mic is connected
  3. Start recording
  4. Stop recording
  5. Save the recorded audio file
  6. Play the saved voice file

After you init your audio session, you could use a method like the one below to save an a recording to a given filename:

-(void) saveAudioFileNamed:(NSString *)filename {

destinationString = [[self documentsPath] stringByAppendingPathComponent:filename];
NSLog(@"%@", destinationString);
NSURL *destinationURL = [NSURL fileURLWithPath: destinationString];

NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
                          [NSNumber numberWithFloat: 44100.0],                 AVSampleRateKey,
                          [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
                          [NSNumber numberWithInt: 1],                         AVNumberOfChannelsKey,
                          [NSNumber numberWithInt: AVAudioQualityMax],         AVEncoderAudioQualityKey,
                          nil];

NSError *error;

recorder = [[AVAudioRecorder alloc] initWithURL:destinationURL settings:settings error:&error];
recorder.delegate = self;
}
Share:
13,191
Lendo92
Author by

Lendo92

Updated on June 04, 2022

Comments

  • Lendo92
    Lendo92 almost 2 years

    In my App, I want the user to be able to record one sound file and play it back, and then save the sound file for later. I used this tutorial to set up the play and record interface, but this tutorial leaves out a key part: how do I permanently save the sound to the disk?

    Also, while you're here, how do I set a maximum time for the sound file to record? I don't want sound files exceeding 30 seconds in length.

    Thanks, hopefully I can get this all sorted out.