【问题标题】:Can't Play AVAudioRecording with AVAudio Player after leaving view离开视图后无法使用 AVAudioPlayer 播放 AVAudioRecorder
【发布时间】:2012-08-10 23:34:16
【问题描述】:

我正在尝试创建一个应用程序,允许某人在 iOS5 中永久录制和保存他们的声音。然后可以随时播放该录音。只要用户停留在记录和播放音频的视图上,我的代码就可以工作,但是当我离开视图时,回到它并尝试播放音频时,我会收到此错误:

错误:操作无法完成。 (OSStatus 错误 -50。)

显然,离开视图会导致保存在 Documents 文件夹中的文件无法正常工作?有人对我如何解决这个问题有解决方案或答案吗?

我录制和播放音频的代码如下:

- (IBAction)playSound:(id)sender {

    if (!self.audioRecorder.recording)
    {
        NSError *error;

        if (self.audioPlayer)
            self.audioPlayer = nil;

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

        self.audioPlayer.delegate = self;
        [self.audioPlayer prepareToPlay];

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

}


- (IBAction)recordSound:(id)sender {

    if(!self.audioRecorder.recording)
    {

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

        self.soundFileURL = [NSURL fileURLWithPath:self.soundFilePath];

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

        NSError *error = nil;

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

        if (error)
        {
            NSLog(@"error: %@", [error localizedDescription]);
        } else {
            [self.audioRecorder prepareToRecord];
        }


        [self.audioRecorder record];
        [self.audioRecorder recordForDuration:(NSTimeInterval) 5];
        [self.record setTitle:@"Stop" forState: UIControlStateNormal];
    }
    else if (self.audioRecorder.recording)
    {
        [self.audioRecorder stop];
        [self.record setTitle:@"Record" forState: UIControlStateNormal];

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

【问题讨论】:

    标签: objective-c ios5 avaudioplayer avaudiorecorder


    【解决方案1】:

    我猜这是因为当你录制文件时,你指定了一个文件名,并且 url 保存在 self.audioRecorder.url 属性中。

    离开视图后,AVAudioPlayer 对象 self.audioPlayerself.audioRecorder 将从内存中释放。

    当您返回时,尝试使用属性 self.audioRecorder.url 对其进行初始化,但实际上并不存在,因此使用 nil 进行初始化。

    尝试从这里改变你的 alloc 和 init:

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

    到这里:

        self.dirPaths = NSSearchPathForDirectoriesInDomains(
                                                       NSDocumentDirectory, NSUserDomainMask, YES);
        self.docsDir = [self.dirPaths objectAtIndex:0];
        self.soundFilePath = [self.docsDir
                                   stringByAppendingPathComponent:@"sound.caf"];
    
        self.soundFileURL = [NSURL fileURLWithPath:self.soundFilePath];
        self.audioPlayer = [[AVAudioPlayer alloc] 
                   initWithContentsOfURL:self.soundFileURL                                    
                   error:&error];
    

    或类似的东西,只要你用存储在文件系统中的文件初始化播放器。不是来自存储在内存中的对象

    【讨论】:

    • 我已经用 AVAudioRecorder 做了 soundFilePath 等。此外,在 audioPlayer 上方添加说明也不起作用。声音文件(或类似的东西)仍然存在,因为我收到了那个错误,所以很明显它正在读取一些东西。它只是无法播放文件。
    • 如果有效,请考虑接受答案。投票也不会伤害:)
    猜你喜欢
    • 1970-01-01
    • 2011-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多