0

誰かが自分の声を録音して永久に保存できるアプリケーションを 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];
    }
}
4

1 に答える 1

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

または、ファイル システムに保存されているファイルを使用してプレーヤーを初期化する限り、似たようなものです。メモリに保存されたオブジェクトからではありません

于 2012-08-10T23:52:51.077 に答える