문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

Use NSFileManager to move the file.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top