录制audio文件并保存在iPhone本地

我一直在寻找这个地方的答案,但无法find我所需要的。 基本上在我的应用程序,我录制语音文件(如iOS语音备忘录应用程序),然后将其保存到本地文件目录。 由于某种原因,下次我启动应用程序时,录制文件的URL就会过期。 此外,即使没有,如果我logging两次,第二个文件的URL获得相同的URL作为第一个,所以我失去了第一个文件。

这样录制:

[audioRecorder record]; 

其中:AVAudioRecorder * audioRecorder;

玩是好的:

  [audioPlayer play]; 

其中:AVAudioPlayer * audioPlayer;

logging语音备忘录并将其保存到iPhone上的本地磁盘的最佳方法是什么?

谢谢。

更新:

我试图使用这个代码:

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName]; BOOL status = [data writeToFile:filePath atomically:YES]; 

数据是我的AVAudioPlayer NSData属性的数据,但BOOL得到0,不知道为什么。

返回我们用作声音文件名的当前date和时间。

 - (NSString *) dateString { // return a formatted string for a file name NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"ddMMMYY_hhmmssa"; return [[formatter stringFromDate:[NSDate date]] stringByAppendingString:@".aif"]; } 

设置audio会话

 - (BOOL) startAudioSession { // Prepare the audio session NSError *error; AVAudioSession *session = [AVAudioSession sharedInstance]; if (![session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error]) { NSLog(@"Error setting session category: %@", error.localizedFailureReason); return NO; } if (![session setActive:YES error:&error]) { NSLog(@"Error activating audio session: %@", error.localizedFailureReason); return NO; } return session.inputIsAvailable; } 

录制声音..

 - (BOOL) record { NSError *error; // Recording settings NSMutableDictionary *settings = [NSMutableDictionary dictionary]; [settings setValue: [NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey]; [settings setValue: [NSNumber numberWithFloat:8000.0] forKey:AVSampleRateKey]; [settings setValue: [NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey]; [settings setValue: [NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey]; [settings setValue: [NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey]; [settings setValue: [NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey]; [settings setValue: [NSNumber numberWithInt: AVAudioQualityMax] forKey:AVEncoderAudioQualityKey]; NSArray *searchPaths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentPath_ = [searchPaths objectAtIndex: 0]; NSString *pathToSave = [documentPath_ stringByAppendingPathComponent:[self dateString]]; // File URL NSURL *url = [NSURL fileURLWithPath:pathToSave];//FILEPATH]; // Create recorder recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error]; if (!recorder) { NSLog(@"Error establishing recorder: %@", error.localizedFailureReason); return NO; } // Initialize degate, metering, etc. recorder.delegate = self; recorder.meteringEnabled = YES; //self.title = @"0:00"; if (![recorder prepareToRecord]) { NSLog(@"Error: Prepare to record failed"); //[self say:@"Error while preparing recording"]; return NO; } if (![recorder record]) { NSLog(@"Error: Record failed"); // [self say:@"Error while attempting to record audio"]; return NO; } // Set a timer to monitor levels, current time timer = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(updateMeters) userInfo:nil repeats:YES]; return YES; } 

播放声音…从文档directiory中检索

 -(void)play { NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentPath_ = [searchPaths objectAtIndex: 0]; NSFileManager *fileManager = [NSFileManager defaultManager]; if ([fileManager fileExistsAtPath:[self recordingFolder]]) { arrayListOfRecordSound=[[NSMutableArray alloc]initWithArray:[fileManager contentsOfDirectoryAtPath:documentPath_ error:nil]]; NSLog(@"====%@",arrayListOfRecordSound); } NSString *selectedSound = [documentPath_ stringByAppendingPathComponent:[arrayListOfRecordSound objectAtIndex:0]]; NSURL *url =[NSURL fileURLWithPath:selectedSound]; //Start playback player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; if (!player) { NSLog(@"Error establishing player for %@: %@", recorder.url, error.localizedFailureReason); return; } player.delegate = self; // Change audio session for playback if (![[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error]) { NSLog(@"Error updating audio session: %@", error.localizedFailureReason); return; } self.title = @"Playing back recording..."; [player prepareToPlay]; [player play]; } 

stopRecording

 - (void) stopRecording { // This causes the didFinishRecording delegate method to fire [recorder stop]; } 

continueRecording

 - (void) continueRecording { // resume from a paused recording [recorder record]; } 

pauseRecording

  - (void) pauseRecording { // pause an ongoing recording [recorder pause]; }