0

Newbie iOS coder here, apologies if the answer is really simple.

So I set up my audio recording in viewDidLoad

// 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];

I have a bar button that records new audio files:

 // 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];

} else {

    // Pause recording
    [recorder pause];
}

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Stop" style:UIBarButtonItemStylePlain target:self
                                                                         action:@selector(stopTapped)];

Then the start becomes a stop button

[recorder stop];

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setActive:NO error:nil];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"New" style:UIBarButtonItemStylePlain target:self
                                                                         action:@selector(actionNew)];

How can I add this as a PFFile and save in a dictionary in Parse? I've read through a lot of the Parse documentation but still don't really get the hang of it. Any help much appreciated.

atwalsh
  • 3,622
  • 1
  • 19
  • 38
jane
  • 55
  • 4

1 Answers1

0

It looks like the simplest solution is to load the file from the phone when the user is finished recording instead of trying to save the recording directly to an NSData object. If you want to look into recording straight to an NSData object check out apple docs and this stackoverflow question.

First you want to get an instance of NSData after the user has finished recording :

NSData *audioData = [[NSData alloc] initWithContentsOfFile:filePath];

Make a new PFFile :

PFFile *file = [PFFile fileWithName:@"resume.txt" data:data];

And then save it with your desired saving method.

Community
  • 1
  • 1
hhanesand
  • 990
  • 11
  • 28