AVAudioRecorder不保存录音 [英] AVAudioRecorder not saving recording

查看:112
本文介绍了AVAudioRecorder不保存录音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作iOS游戏.我需要做的一件事情是允许用户进行快速的小录音.所有这些都可以,但是录音只是临时保存.因此,当用户关闭应用程序并重新打开它时,录音应该可以再次播放,但不能播放,当我关闭应用程序时,该记录会被删除.我不明白我在做什么错.下面是我的代码:

I am making an iOS game. One of the things I need to do is to allow the user to make a quick little audio recording. This all works, but the recording is only temporarily saved. So when the user closes the app and reopens it, the recording should be able to play again, but it doesn't, it gets deleted when I close the app. I don't understand what I am doing wrong. Below is my code:

我像这样在ViewDidLoad方法中设置AVAudioRecorder:

I setup the AVAudioRecorder in the ViewDidLoad method like this:

// 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链接的IBAction:

When I want to play, record or stop the audio, I have made the following IBActions which are linked to UIButtons:

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

如果您尝试使用我的代码,您会发现它可以工作,但似乎只是暂时保存了音频文件.

If you try my code you will see that it works, but it only seems to save the audio file temporarily.

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

What am I doing wrong? I thought I had used all the correct AVAudioRecorder methods?

推荐答案

好,非常感谢@Neeku回答了我的问题,当然要考虑一些问题,但仍然不能解决问题.我在四处搜寻,发现这个例子运行得很好,更重要的是,这向我展示了我以错误的方式使用了全部功能.我认为我的另一个问题是我的应用程序将删除ViewDidload方法中以前保存的音频文件.而且,我认为我没有正确使用AVAudioSession实例.

Ok so thanks for @Neeku so much for answering my question, certainly something to take into account but its still not solving the problem. I was searching around and I found this example which works perfectly and more to the point shows me that I was approaching this entire functionality the wrong way. I think another one of my problems is that my app would delete the previously saved audio file in the ViewDidload method. And as well as that I don't think I have used the AVAudioSession instances correctly.

无论如何,我发现的示例非常有用并且可以完美地解决我的问题,您可以在这里找到它:

Anyway the example I found is very useful and solves my problem perfectly, you can find it here: Record audio and save permanently in iOS

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

Hope that helps anyone else who is having a similar problem to me.

这篇关于AVAudioRecorder不保存录音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆