AVAudioRecorder 不保存录音

AVAudioRecorder not saving recording

我正在制作 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. 确保麦克风 connected/working
  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 回答我的问题,当然有一些需要考虑的问题,但它仍然没有解决问题。我四处搜索,发现这个示例非常有效,而且更重要的是,我向我展示了我以错误的方式实现了整个功能。我认为我的另一个问题是我的应用程序会删除之前在 ViewDidload 方法中保存的音频文件。而且我认为我没有正确使用 AVAudioSession 实例。

无论如何我找到的例子非常有用并且完美地解决了我的问题,你可以在这里找到它:Record audio and save permanently in iOS

希望对遇到与我类似问题的其他人有所帮助。