保存音频文件并在视图之间导航

时间:2012-05-20 16:01:14

标签: iphone objective-c ios audio

我在保存录制的音频文件时遇到问题,并在浏览视频后播放它们。

我一直在使用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 :导航栏中的后退按钮也会发生这种情况。

1 个答案:

答案 0 :(得分:0)

您正在使用audioPlayer分配audioRecorder.url。如果发生某事audioRecorder,则url可能无效。

尝试以这种方式设置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];
    }
}