0

What is the method by which I can set an NSManagedObject as the destination for the AVAudioRecorder (as opposed to NSURL)? I would like to save tiny sound bites as the binary core data type with the "external storage" option selected... and of course play them back. I've seen several questions that skirt the subject, and it seems there are some out there that are doing this, but I've yet to find any hardcore code examples. So far I've had success with the basic file type recording by following this tutorial : http://www.supersuraccoon-cocos2d.com/2011/06/05/voice-record-demo/ , I would just like to take it directly to core data.

FYI - I do realize best practice is to store the URL, and then retrieve, but I'd really like to go directly with the binary into CoreData if possible.

thanks!!

p.s. If you've got a sample in Swift, it would be greatly appreciated!

CodeNoob
  • 250
  • 3
  • 9
  • I don't think this is possible. If you've seen others that claim to be doing this, please post some URLs where they discuss it. – Tom Harrington Jul 25 '14 at 16:20
  • Thanks for the response, Tom. I may be misinterpreting this, but here is a URL http://stackoverflow.com/questions/4649918/avaudiorecorder-avaudioplayer-append-recording-to-file – CodeNoob Jul 25 '14 at 16:37

1 Answers1

3

you need save your Audio as NSData. You need get the file save in your disk and convert to NSData.

I have a function that configure My Audio, and one second function that make conversion!!!

-(void)persistDataAudioPlaylet{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,      YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"myAudio.caf"];
    NSData *fileContents = [NSData dataWithContentsOfFile:filePath];
    self.playlet.sound = fileContents;
}


-(void)configureAudioRecord{
     NSArray *dirPaths;
     NSString *docsDir;

     dirPaths = NSSearchPathForDirectoriesInDomains(
                                               NSDocumentDirectory, NSUserDomainMask, YES);
     docsDir = dirPaths[0];

     NSString *soundFilePath = [docsDir
                           stringByAppendingPathComponent:@"myAudio.caf"];

     NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

     NSDictionary *recordSettings = [NSDictionary
                                dictionaryWithObjectsAndKeys:
                                [NSNumber numberWithInt:AVAudioQualityMin],
                                AVEncoderAudioQualityKey,
                                [NSNumber numberWithInt:16],
                                AVEncoderBitRateKey,
                                [NSNumber numberWithInt: 2],
                                AVNumberOfChannelsKey,
                                [NSNumber numberWithFloat:44100.0],
                                AVSampleRateKey,
                                nil];

     NSError *error = nil;

     AVAudioSession *audioSession = [AVAudioSession sharedInstance];
     [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord
                    error:nil];

     self.audioRecorder = [[AVAudioRecorder alloc]
                  initWithURL:soundFileURL
                  settings:recordSettings
                  error:&error];

    if (error)
    {
        NSLog(@"error: %@", [error localizedDescription]);
    } else {
        [self.audioRecorder prepareToRecord];
    }
}