在路径上保存AVAudioRecording

时间:2013-08-26 22:43:51

标签: ios

我设法设置了AVAudiorecorder来制作录音,但是我想将录音保存在文档目录中以便以后收听。我试过这个:

- (void) audioRecorderDidFinishRecording:(AVAudioRecorder *)avrecorder successfully:(BOOL)flag{

ButtonStopRecording.hidden = YES;

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"audioFile.ext"];  // Where ext is the audio format extension you have recorded your sound
[avrecorder.data writeToFile:filePath atomically:YES];

} 

中有错误
avrecorder.data

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:0)

AVAudioRecorder没有数据属性。您只需要在初始化AVAudioRecorder时设置保存录制音频的位置。设置录像机时,只需将其设置到文档目录即可。

查看AVAudioRecorder

上的苹果文档

答案 1 :(得分:0)

尝试使用

访问该文件
avrecorder.url

假设您在开始record

时正确设置了录像机

像这样......

NSString *temnpVoiceFile = [NSString pathWithComponents:[NSArray arrayWithObjects:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject], @"tempVoice.caf", nil]];

// We need to update the file generator
NSURL *soundFileURL = [NSURL fileURLWithPath:temnpVoiceFile];

NSError *error = nil;

// Instantiate an audio recorder.
self.audioRecorder = [[[AVAudioRecorder alloc] initWithURL:soundFileURL settings:_recordSettings error:&error] autorelease];
NSAssert1(!error, @"error creating audio file = %@", error);

BOOL fileSuccess = [_audioRecorder prepareToRecord];

NSAssert(fileSuccess, @"audio recorder could not be prepared.");
[_audioRecorder record];

如果您需要更多信息,请与我们联系。

答案 2 :(得分:0)

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"audioFile.ext"];  // Where ext is the audio format extension you have recorded your sound
NSData *data=[NSData dataWithContentsOfURL:avrecorder.url];
    [data writeToFile:soundFilePath atomically:YES];
[data writeToFile:filePath atomically:YES];

我认为这会对你有所帮助。