Question

I’m want to save a recorded file to my apps documents but can’t figure out how to do it. I have made a recording and it’s stored in a temporary file and i’m able to play that sound. Now i want to save that file in a new name to the apps document when i leave that view.

Code to record the sound:

NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDir
                               stringByAppendingPathComponent:@"tmpSound.caf"];

tempRecFile = [NSURL fileURLWithPath:soundFilePath];

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

recorder = [[AVAudioRecorder alloc] initWithURL:tempRecFile settings:recSettings error:nil];
[recorder setDelegate:self];
[recorder prepareToRecord];
[recorder record];

Do any one know how to do this or if it’s even possible to do this?

Was it helpful?

Solution

You should be able to do this with something like:

NSError *error;
[[NSFileManager defaultManager] copyItemAtURL:tempRecFile toURL:newURL error:&error];

Or if you don't care about errors:

[[NSFileManager defaultManager] copyItemAtURL:tempRecFile toURL:newURL error:nil];

A word of warning though - you should think carefully about whether you really need to save this audio in the documents folder, or if the caches folder would be sufficient. Apple have strict rules about what is allowed.

OTHER TIPS

Use NSFileManager to move the file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top