0

在视图之间导航后,我无法保存录制的音频文件并播放它们。

我一直在使用http://www.techotopia.com/index.php/Recording_Audio_on_an_iPhone_with_AVAudioRecorder_%28iOS_4%29上的教程

我已经添加了一行来保存音频文件,所以我prepareForAudioRecording看起来像下面的代码。这个方法在viewDidLoad.

-(void) prepareForAudioRecording
{
    btnPlay.enabled = NO;
    btnStop.enabled = NO;

    NSArray *dirPaths;
    NSString *docsDir;

    dirPaths = NSSearchPathForDirectoriesInDomains(
                                                   NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = [dirPaths objectAtIndex:0];
    NSString *soundFilePath = [docsDir
                               stringByAppendingPathComponent:@"page1.caf"];

    //this line added to save the audio file
    [audioPlayer.data writeToFile:soundFilePath atomically:YES];


    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

    NSDictionary *recordSettings = [NSDictionary 
                                    dictionaryWithObjectsAndKeys:
                                    [NSNumber numberWithInt:AVAudioQualityMin],
                                    AVEncoderAudioQualityKey,
                                    [NSNumber numberWithInt:16], 
                                    AVEncoderBitRateKey,
                                    [NSNumber numberWithInt: 2], 
                                    AVNumberOfChannelsKey,
                                    [NSNumber numberWithFloat:44100.0], 
                                    AVSampleRateKey,
                                    nil];

    NSError *error = nil;

    audioRecorder = [[AVAudioRecorder alloc]
                     initWithURL:soundFileURL
                     settings:recordSettings
                     error:&error];

    if (error)
    {
        NSLog(@"error: %@", [error localizedDescription]);

    } else {
        [audioRecorder prepareToRecord];
    }
}

正如我所说,我可以录制音频,然后在同一个视图控制器中播放音频,但是当我离开视图并返回它时,我无法播放录音。

首先,播放和停止按钮被禁用,我认为这应该只在第一次加载视图时发生 - 但即使我启用它们并按下播放按钮,也没有音频。

下面是我的播放、停止和录制按钮的其他方法。

- (IBAction)playAudio:(id)sender {
    if (!audioRecorder.recording)
    {
        btnStop.enabled = YES;
        btnRecord.enabled = NO;

        //if (audioPlayer)
            //[audioPlayer release];
        NSError *error;

        audioPlayer = [[AVAudioPlayer alloc] 
                       initWithContentsOfURL:audioRecorder.url                                    
                       error:&error];

        audioPlayer.delegate = self;

        if (error)
            NSLog(@"Error: %@", 
                  [error localizedDescription]);
        else
            [audioPlayer play];
    }
}

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    btnRecord.enabled = YES;
    btnStop.enabled = NO;
}
-(void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
{
    NSLog(@"Decode Error occurred");
}
-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag
{
}
-(void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder error:(NSError *)error
{
    NSLog(@"Encode Error occurred");
}


- (IBAction)recordAudio:(id)sender {
    if (!audioRecorder.recording)
    {
        btnPlay.enabled = NO;
        btnStop.enabled = YES;
        [audioRecorder record];
    }
}

- (IBAction)stopAudio:(id)sender {
    btnStop.enabled = NO;
    btnPlay.enabled = YES;
    btnRecord.enabled = YES;

    if (audioRecorder.recording)
    {
        [audioRecorder stop];
    } else if (audioPlayer.playing) {
        [audioPlayer stop];
    }
}

我知道那里有很多代码要查看,但如果我遗漏了什么,请告诉我。我真的很感激这方面的任何帮助,谢谢。

编辑1:好的,我做了更多调查并打开了保存我的音频文件的文件夹。音频记录良好,文件大小约为 400KB。当我推送到下一个视图控制器时,文件大小保持在 400KB。但是,一旦我按下后退按钮(不是导航栏中预编程的按钮),文件大小就会变为 4KB;一个空文件。我的后退按钮的代码很简单:

- (IBAction)backPressed:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

请帮忙!

编辑 2:导航栏中的后退按钮也会发生这种情况。

4

1 回答 1

0

您正在audioPlayer分配audioRecorder.url. 如果发生了什么事audioRecorderurl可能是无效的。

尝试audioPlayer这样设置:

- (IBAction)playAudio:(id)sender {
    if (!audioRecorder.recording)
    {
        btnStop.enabled = YES;
        btnRecord.enabled = NO;

        NSArray *dirPaths;
        NSString *docsDir;

        dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        docsDir = [dirPaths objectAtIndex:0];
        NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"page1.caf"];

        NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

        NSError *error;

        audioPlayer = [[AVAudioPlayer alloc] nitWithContentsOfURL:soundFileUR                                    
                                                            error:&error];

        audioPlayer.delegate = self;

        if (error)
            NSLog(@"Error: %@",[error localizedDescription]);
        else
            [audioPlayer play];
    }
}
于 2012-05-20T16:11:26.583 回答