我正在尝试将录制的音频文件保存在文档文件夹中。我尝试使用CoreData
来保存录制的音频,但我无法理解。集成它的最佳方法是什么?我的代码如下: -
if(_radio.isRecording) {
[_recordbutton setImage:[UIImage imageNamed:@"record_off.png"] forState:UIControlStateNormal];
[_radio stopRecording];
} else {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *recordname = [NSString stringWithFormat:@"%@%d.%@", [_detailItem objectForKey:@"name"], _recordingCounter++, [_radio fileExtensionHint]];
NSString *path = [documentsPath stringByAppendingPathComponent:recordname];
[_recordbutton setImage:[UIImage imageNamed:@"record_on.png"] forState:UIControlStateNormal];
[_radio startRecordingWithDestination:path];
}
}
- (void)radio:(MeroRadio *)radio didStopRecordingWithDestination:(NSString *)path {
NSLog(@"Did stop recording with destination: %@", path);
[_recordbutton setImage:[UIImage imageNamed:@"record_off.png"] forState:UIControlStateNormal];
NSURL *url = [NSURL fileURLWithPath:path];
[_recordings saveobject:[url lastpathcomponent];
}
_recordings
是NSMutableArray
。那么保存它的最佳方法是什么?
答案 0 :(得分:0)
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
NSArray *documentsFolders = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[documentsFolders objectAtIndex:0] stringByAppendingPathComponent:@"Recording.aiff"];
[library writeVideoAtPathToSavedPhotosAlbum:[NSURL URLWithString:path] completionBlock:^(NSURL *assetURL, NSError *error){
/*notify of completion*/
NSLog(@"AssetURL: %@",assetURL);
NSLog(@"Error: %@",error);
if (!error) {
//video saved
[[self appDelegate] showAlertWithTitle:@"Audio Saved" message:@""];
}
else{
[[self appDelegate] showAlertWithTitle:@"Error" message:error.domain];
}
}];