0

我设法设置了 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

我该如何解决这个问题?

4

3 回答 3

1

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

查看AVAudioRecorder上的苹果文档

于 2013-08-26T22:54:17.907 回答
1
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];

我想这会对你有所帮助。

于 2013-08-27T05:02:23.360 回答
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];

如果您需要更多信息,请告诉我。

于 2013-08-27T04:47:13.493 回答