iOS Audio

By Nicholas Duchon - Dec 10, 2013

References:

Steps outline:

  1. Include the following minimal framework:
    1. AVFoundationFramework
  2. Import the following header files:
    1. #import <AVFoundation/AVFoundation.h>
    2. #import <CoreAudio/CoreAudioTypes.h>
    3. (optional) to support delegates, for example responding to end of playback:
      @interface auaViewController : UIViewController
      <AVAudioRecorderDelegate, AVAudioPlayerDelegate>
  3. Record
    1. Open file
      1. the following directory will work on a cell phone and the simulator:
        soundFileURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingString:fileName]];
      2. The following, home directory, will only work with the simulator:
        soundFileURL = [NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingString:fileName]];
    2. Set audio settings
    3. Set up the recorder
    4. Record and stop audio
  4. Playback
    1. Open file
    2. Play the file

Example:

In the following example, the storyboard has two buttons, one to start and stop a recording (record), and the other to play it back (play).
The sound is recorded to a file, sound.aac.

Examining parts of the view controller file:

Data shared among methods:

Here are the relevant methods:

The view controller file:

//
//  auaViewController.m
//  Audio One
//
//  Created by Nick Duchon on 12/10/13.
//  Copyright (c) 2013 Nick Duchon. All rights reserved.
//

#import "auaViewController.h"

@interface auaViewController () {
    AVAudioRecorder * audioRecorder;
    NSURL * soundFileURL;
    AVAudioPlayer * audioPlayer;
}
@end

@implementation auaViewController

bool recording = FALSE;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self setupRecorder];
} // end method viewDidLoad

- (void) setupRecorder {
    NSString * fileName = @"sound.aac";
    soundFileURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingString:fileName]];
    NSDictionary * soundSetting = @{AVSampleRateKey:@44100.0f,
                                    AVFormatIDKey:@(kAudioFormatMPEG4AAC),
                                    AVNumberOfChannelsKey:@2,
                                    AVEncoderAudioQualityKey:@(AVAudioQualityHigh)
                                    }; // end sound settings
    audioRecorder = [[AVAudioRecorder alloc]
                     initWithURL:soundFileURL
                     settings:soundSetting
                     error:Nil];
} // end method setupRecorder

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)record:(id)sender {
    if (recording) {
        [audioRecorder stop];
        NSLog(@"Stopped");
        [self.recordButton setTitle:@"Start Recording" forState:UIControlStateNormal];
        recording = FALSE;
    } else {
        [audioRecorder record];
        NSLog(@"Recording");
        [self.recordButton setTitle:@"End Recording" forState:UIControlStateNormal];
        recording = true;
    } // end if, case not recording
} // end method record

- (IBAction)play:(id)sender {
    audioPlayer = [[AVAudioPlayer alloc]
                   initWithContentsOfURL:soundFileURL
                   error:nil];
    audioPlayer.delegate = self;
    [audioPlayer play];
    NSLog(@"Playing");
} // end method play

- (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
    NSLog(@"Done Playing");
}

@end