AVAudioRecorder不保存录音

我正在制作iOS游戏。 我需要做的一件事就是让用户快速录制一些小录音。 这一切都有效,但录音只是暂时保存。 因此,当用户关闭应用程序并重新打开它时,录制应该能够再次播放,但事实并非如此,当我关闭应用程序时它会被删除。 我不明白我做错了什么。 以下是我的代码:

我在ViewDidLoad方法中设置了AVAudioRecorder ,如下所示:

 // Setup audio recorder to save file. NSArray *pathComponents = [NSArray arrayWithObjects:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject], @"MyAudioMemo.m4a", nil]; NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents]; // Setup audio session. AVAudioSession *session = [AVAudioSession sharedInstance]; [session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init]; [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey]; [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey]; [recordSetting setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey]; audio_recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:nil]; audio_recorder.delegate = self; audio_recorder.meteringEnabled = YES; [audio_recorder prepareToRecord]; 

我也有AVAudio委托方法:

 -(void)audio_playerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag { NSLog(@"Did finish playing: %d", flag); } -(void)audio_playerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error { NSLog(@"Decode Error occurred"); } -(void)audio_recorderDidFinishRecording:(AVAudioPlayer *)recorder successfully:(BOOL)flag { NSLog(@"Did finish recording: %d", flag); } -(void)audio_recorderEncodeErrorDidOccur:(AVAudioPlayer *)recorder error:(NSError *)error { NSLog(@"Encode Error occurred"); } 

当我想播放,录制或停止音频时,我做了以下与UIButtons链接的IBActions:

 -(IBAction)play_audio { NSLog(@"Play"); if (!audio_recorder.recording){ audio_player = [[AVAudioPlayer alloc] initWithContentsOfURL:audio_recorder.url error:nil]; [audio_player setDelegate:self]; [audio_player play]; } } -(IBAction)record_voice { NSLog(@"Record"); if (!audio_recorder.recording) { AVAudioSession *session = [AVAudioSession sharedInstance]; [session setActive:YES error:nil]; // Start recording. [audio_recorder record]; } else { // Pause recording. [audio_recorder pause]; } } -(IBAction)stop_audio { NSLog(@"Stop"); [audio_recorder stop]; AVAudioSession *audioSession = [AVAudioSession sharedInstance]; [audioSession setActive:NO error:nil]; } 

如果您尝试我的代码,您将看到它的工作原理,但它似乎只是临时保存音频文件。

我究竟做错了什么? 我以为我使用了所有正确的AVAudioRecorder方法?

要制作工作记录器并保存录制的文件,您需要:

  1. 创建一个新的音频会话
  2. 确保麦克风已连接/正常工作
  3. 开始录制
  4. 停止录音
  5. 保存录制的音频文件
  6. 播放保存的语音文件

您错过了代码中的第5步,因此刚刚录制的文件仍然可供您播放,但是一旦您关闭应用程序,因为它没有保存到应用程序目录中的某个实际文件中,您就会丢失它。 您应该添加一种方法将录制的音频保存到文件中,以便以后随时可以访问它:

 -(void) saveAudioFileNamed:(NSString *)filename { destinationString = [[self documentsPath] stringByAppendingPathComponent:filename]; NSLog(@"%@", destinationString); NSURL *destinationURL = [NSURL fileURLWithPath: destinationString]; NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithFloat: 44100.0], AVSampleRateKey, [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey, [NSNumber numberWithInt: 1], AVNumberOfChannelsKey, [NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey, nil]; NSError *error; audio_recorder = [[AVAudioRecorder alloc] initWithURL:destinationURL settings:settings error:&error]; audio_recorder.delegate = self; } 

与此问题无关,但一般要提及的是,在定义变量等时必须遵循Apple(Objective-C)的命名约定audio_recording绝不遵循这些准则。 您可以使用audioRecording东西。

好的,非常感谢@Neeku回答我的问题,当然有些事情需要考虑,但仍然无法解决问题。 我正在四处寻找,我发现这个例子非常有效,而且更多的是向我展示我正以错误的方式接近整个function。 我认为我的另一个问题是我的应用程序会删除ViewDidload方法中以前保存的音频文件。 而且我认为我没有正确使用AVAudioSession实例。

无论如何,我发现的例子非常有用并且完美地解决了我的问题,你可以在这里找到它: 录制音频并永久保存在iOS中

希望能帮助其他遇到类似问题的人。