0
嗨,我正在创建可以录制声音的应用程序。我可以有录制的声音文件,我可以立即播放它。我不知道如何将它保存在iPhone上,以便在重新打开应用程序后播放它。如何保存使用AVAudioRecorder录制的音频数据以备后用?
有没有什么办法将这个文件作为应用程序保存数据存储?
在此先感谢。
嗨,我正在创建可以录制声音的应用程序。我可以有录制的声音文件,我可以立即播放它。我不知道如何将它保存在iPhone上,以便在重新打开应用程序后播放它。如何保存使用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
[yourAudioPlayer.data writeToFile:filePath atomically:YES];
谢谢!有效。 – KodeKiller
当你说“有录音文件”,你是如何存储/在你的代码中使用它?它是一个NSData对象还是它的某种对象? –
我拿NSURL * soundFileURL = [NSURL fileURLWithPath:soundFilePath];然后audioRecorder = [[AVAudioRecorder alloc] initWithURL:soundFileURL settings:recordSettings error:&error];每当我播放录制的声音我做audioPlayer = [[AVAudioPlayer分配] initWithContentsOfURL:audioRecorder.url 错误:&错误]; – KodeKiller