保存AVAudioRecording路径

保存AVAudioRecording路径

问题描述:

我设法设置AVAudiorecorder进行录制,但我想将录制文件保存在文档目录中以稍后收听。我尝试这样做:保存AVAudioRecording路径

- (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 

错误我该如何解决这个问题?

+0

现在您必须知道,如果您发布有关错误的问题,则应在问题中包含完整的错误。 – rmaddy

尝试访问使用

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

让我知道你是否需要更多信息。

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

退房苹果文档上AVAudioRecorder

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

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