iphone – 滑块在播放音频结束时停止

在我的项目中,我使用AVAudioPlayer来播放我保存的音频.我正在显示用于播放音频的滑块.使用播放器的当前时间属性设置滑块的值.

playSlider.value = avAudioPlayer.currentTime;

这是计时器代码

updateTimer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(updateCurrentTime) userInfo:p repeats:YES];



//Calls the update current time function 
- (void)updateCurrentTime
{
    [self updateCurrentTimeForPlayer:self.player];
}


//Updates the current time while the audio is playing
-(void)updateCurrentTimeForPlayer:(AVAudioPlayer *)avAudioPlayer
{
    currentTimeLabel.text = [NSString stringWithFormat:@"%d:%02d", (int)avAudioPlayer.currentTime / 60, (int)avAudioPlayer.currentTime % 60, nil];
    playSlider.value = avAudioPlayer.currentTime;

    if (playSlider.value==playSlider.minimumValue) 
    {
        moveToBeginningButton.enabled = NO;
        rewindButton.enabled = NO;
        NSLog(@"PlaySliderMaxValue1 === %f",playSlider.maximumValue);
        NSLog(@"PlaySliderValue1 === %f",playSlider.value);
    }
    else  
    {  
        moveToBeginningButton.enabled = YES;
        rewindButton.enabled = YES; 
        NSLog(@"PlaySliderMaxValue2 === %f",playSlider.maximumValue);
        NSLog(@"PlaySliderValue2 === %f",playSlider.value);

    }
}

滑块的最大值设置如下

playSlider.maximumValue = audioPlayer.duration;

我遇到的问题是,在播放后滑块点将始终返回到音频的开头.这是一个错误吗?我怎么能改变这个?请提供您的建议.

最佳答案 我不能说这是不是一个bug,但尝试使用AVAudioPlayer的委托方法.在didFinishPlaying方法中,将Slider值设置为maximumValue.这可能是解决方案之一.

点赞