Objective c Can';离开视野后,不要与AVAudio播放器一起播放AVAudio

Objective c Can';离开视野后,不要与AVAudio播放器一起播放AVAudio,objective-c,ios5,avaudioplayer,avaudiorecorder,Objective C,Ios5,Avaudioplayer,Avaudiorecorder,我正在尝试创建一个应用程序,允许某人在iOS5中永久录制和保存他们的声音。这段录音可以在任何时候播放。只要用户停留在录制和播放音频的视图上,我的代码就可以工作,但当我离开视图,返回视图并尝试播放音频时,我会出现以下错误: 错误:无法完成该操作。(OSStatus错误-50。) 显然,离开视图会导致保存在Documents文件夹中的文件无法正常工作?有没有人知道我如何解决这个问题的方法或答案 我录制和播放音频的代码如下: - (IBAction)playSound:(id)sender {

我正在尝试创建一个应用程序,允许某人在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];
    }
}

我猜这是因为录制文件时,指定了文件名,url保存在
self.audioRecorder.url
属性中

离开视图后,将从内存中释放
AVAudioPlayer
对象
self.audioPlayer
self.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等操作。此外,在音频播放器上方添加说明也不起作用。声音文件(或它的一些外表)仍然存在,因为我得到了那个错误,所以很明显它正在读取一些东西。它不能播放文件。如果它有效,请考虑接受这个答案。此外,一次投票也不会有什么坏处:)