录制完成后保存音频文件?

时间:2013-05-24 00:43:17

标签: ios xcode core-data audio-recording

我正在尝试将录制的音频文件保存在文档文件夹中。我尝试使用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];

}

_recordingsNSMutableArray。那么保存它的最佳方法是什么?

1 个答案:

答案 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];

    }

}];