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

私が言ったように、オーディオを録音してから、同じView Controller内でオーディオを再生できますが、ビューから離れて戻ってくると、録音を再生できません。

まず、再生ボタンと停止ボタンが無効になりました。これは、ビューが最初に読み込まれたときにのみ発生するはずだったのですが、それらを有効にして再生ボタンを押しても、音声がありませんでした。

私の他の方法は、私の再生、停止、および録音ボタンについて以下に示しています。

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

そこには見るべきコードがたくさんあることは知っていますが、何か見逃している場合はお知らせください。これについて何か助けていただければ幸いです、ありがとう。

EDIT 1 : わかりました、もう少し調査を行い、オーディオ ファイルが保存されているフォルダーを開きました。音声は問題なく録音され、ファイル サイズは約 400 KB です。次のView Controllerにプッシュすると、ファイルサイズは400KBのままです。しかし、戻るボタン (ナビゲーション バーに事前にプログラムされたボタンではありません) を押すとすぐに、ファイルサイズは 4KB になります。空のファイル。戻るボタンのコードは次のとおりです。

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

助けてください!

EDIT 2:ナビゲーションバーの戻るボタンでも発生します。

4

1 に答える 1

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];
    }
}
于 2012-05-20T16:11:26.583 に答える