Reputation: 139
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.
Upvotes: 2
Views: 13321
Reputation: 1476
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:
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;
}
Upvotes: 1