-1

I'm trying to implement some simple audio recording functionality in my application and I can't quite figure out to do it. I've been pointed to this example, but I can't get it to run in XCode, and it appears to be written in C++.

What I need to be able to do is record audio to a file, and then be able to get the current timestamp of the recording whilst recording. I would appreciate any help with this. Thanks!

James Harpe
  • 4,315
  • 8
  • 47
  • 74
  • 1
    He never shows a single line of code of his own. See the last 4 topics he has opened. – El Tomato Jan 28 '14 at 20:13
  • So, first of all, this is factually incorrect. I have asked 77 questions, and most of them have lots of my own code. I'm not sure what kind of code you'd like to see for a general question like this. – James Harpe Jan 28 '14 at 23:50

2 Answers2

5

You can record and play audio using the AVFoundation framework. Firstly you will need to implement this within your .h file and add a framework or library's within your xcode project settings like so:

Adding AVFoundation Framwork

After adding into your project settings import AVFoundation into your .h file like so:

#import <AVFoundation/AVFoundation.h>

Now Implement your delegates within your .h file:

@interface ViewController : UIViewController <AVAudioRecorderDelegate, AVAudioPlayerDelegate>

After this declare your AVAudioRecorder and AVAudioPlayer in your .h file like so:

@interface ViewController () {
     AVAudioRecorder *recorder;
     AVAudioPlayer *player;
IBOutlet UIButton *stopButton;
IBOutlet UIButton *playButton ;
}

- (IBAction)recordPauseTapped:(id)sender;
- (IBAction)stopTapped:(id)sender;
- (IBAction)playTapped:(id)sender;

Now set up everything in the -(Void)ViewDidLoad{} :

- (void)viewDidLoad
{
     [super viewDidLoad];

     // Disable Stop/Play button when application launches
     [stopButton setEnabled:NO];
     [playButton setEnabled:NO];

     // Set the audio file
     NSArray *pathComponents = [NSArray arrayWithObjects:
                                [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
                                @"MyAudioMemo.m4a",
                                nil];
     NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];

     // Setup audio session
     AVAudioSession *session = [AVAudioSession sharedInstance];
     [session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

     // Define the recorder setting
     NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];

     [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
     [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
     [recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];

     // Initiate and prepare the recorder
     recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:NULL];
     recorder.delegate = self;
     recorder.meteringEnabled = YES;
     [recorder prepareToRecord];
}

Now Implement the recording Button Like so...

- (IBAction)recordPauseTapped:(id)sender {
     // Stop the audio player before recording
     if (player.playing) {
         [player stop];
     }

     if (!recorder.recording) {
         AVAudioSession *session = [AVAudioSession sharedInstance];
         [session setActive:YES error:nil];

         // Start recording
         [recorder record];
         [recordPauseButton setTitle:@"Pause" forState:UIControlStateNormal];

     } else {

         // Pause recording
         [recorder pause];
         [recordPauseButton setTitle:@"Record" forState:UIControlStateNormal];
     }

     [stopButton setEnabled:YES];
     [playButton setEnabled:NO];
}

Now Implement the StopButton IBAction:

- (IBAction)stopTapped:(id)sender {
     [recorder stop];

     AVAudioSession *audioSession = [AVAudioSession sharedInstance];
     [audioSession setActive:NO error:nil];
}

Next Implement the playTapped IBAction like so:

- (IBAction)playTapped:(id)sender {
     if (!recorder.recording){
         player = [[AVAudioPlayer alloc] initWithContentsOfURL:recorder.url error:nil];
         [player setDelegate:self];
         [player play];
     }
}

Lastly Implement the required AVPlayer Delegate by doing this:

- (IBAction)playTapped:(id)sender {
     if (!recorder.recording){
         player = [[AVAudioPlayer alloc] initWithContentsOfURL:recorder.url error:nil];
         [player setDelegate:self];
         [player play];
     }
}

And that's it! The Finished Product should look something like this... Example

For more info take a look at These Links:

Link1

Link2

Link3

Documentation Links:

Link 1

Hope This Helps.

Community
  • 1
  • 1
Tom
  • 640
  • 1
  • 7
  • 25
  • Thanks! This will be very useful. Do you have an answer for the second part of my question? How to determine, during recording, what the current time elapsed for the recording is? – James Harpe Jan 28 '14 at 23:51
  • You copy-pasted the `- (IBAction)playTapped:(id)sender` segment twice. Could you please show the delegate implementation? – Nickkk Apr 23 '21 at 17:50
1

The above mentioned AVFoundation framework is iOS only. Dealing with audio on OS X is quite painful in the beginning. CoreAudio, while it is one of the best components I worked with, does require some time spent learning and understanding. You may want to consider for exampel using https://github.com/syedhali/EZAudio for your task.

Volker
  • 4,640
  • 1
  • 23
  • 31