0

音声の再生が終了したか確認したいのですが。その場合、ボタンを一時停止ボタンではなく再生ボタンに変更する必要があります。iOSでそれを行うにはどうすればよいですか?

私のコードは次のようになります:

-(void)playAudioFile:(BOOL)status
{
    NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"audio" ofType:@"mp3"];
    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];


    NSError *error;

    if(status)
    {

        if(!audioPlayer)
        {
            audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:&error];
            audioPlayer.currentTime = currentPlayTime;
            audioPlayer.numberOfLoops = 0; //Infinite
            audioDuration = audioPlayer.duration;

            audioPlaySlider.minimumValue = 0;
            audioPlaySlider.maximumValue = audioDuration;
            audioPlaySlider.value = currentPlayTime;

            [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(audioTimer:) userInfo:[NSDictionary dictionaryWithObject:audioPlayer forKey:@"playerObject"] repeats:YES];
        }

        [audioPlayer play];

        if([audioPlayer rate] != 1.0 )
        {
            [audioPlayer play];
        }
        else
        {
            [self->audioPlayBtn setBackgroundImage:[UIImage imageNamed:@"playBtn.png"] forState:UIControlStateNormal];
        }


        NSLog(@"Play duration : %f",audioDuration);
    }
    else
    {
        if(audioPlayer && audioPlayer.playing)
        {
            [audioPlayer pause];                
        }
    }

    NSLog(@"Error for file : %@",error);

}

私はこれを試しました:

            if([audioPlayer rate] != 1.0 )
            {
                [audioPlayer play];
            }
            else
            {
                [self->audioPlayBtn setBackgroundImage:[UIImage imageNamed:@"playBtn.png"] forState:UIControlStateNormal];
            }

しかし、機能していません..いくつかのガイダンスが必要です...

4

1 に答える 1

1

AVAudioPlayerDelegateのメソッドを使用します

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag

audioplayerインスタンスを持つviewcontroller/classは、オーディオプレーヤーのデリゲートである必要があります。このメソッドは、オーディオプレーヤーの再生が終了したときに呼び出されます。

http://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVAudioPlayerDelegateProtocolReference/Reference/Reference.html

于 2013-03-19T15:26:49.357 に答える