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

发布于 2024-12-15 09:20:09 字数 1385 浏览 1 评论 0原文

在我的项目中,我使用 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;

我遇到的问题是播放后滑块点总是返回到音频的开头。这是一个错误吗?我怎样才能改变这个?请提供您的建议。

In my project I am using an AVAudioPlayer to play my saved audio.I am showing the slider for playing audio. The value of the slider is set using the current time property of the player.
playSlider.value = avAudioPlayer.currentTime;
This is the timer code

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

    }
}

The maximum value of slider is set as follows

playSlider.maximumValue = audioPlayer.duration;

The problem that I am experiencing is that the slider point will always return to the beginning of the audio after playing.Is this a bug? How can I change this? Please provide your suggestion.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离笑几人歌 2024-12-22 09:20:09

我不能说这是否是一个错误,但尝试使用 AVAudioPlayer 的委托方法。在didFinishPlaying方法中将Slider值设置为maximumValue。这可能是解决方案之一。

I cannot say whether this is a bug or not, But try using delegate methods of AVAudioPlayer. In didFinishPlaying method set Slider value to maximumValue. This could be one of the solution.

撩发小公举 2024-12-22 09:20:09

在将 avAudioPlayer.currentTime 的值分配给playslider.value 之前打印它
我认为最终它会变成零。它会将零分配给playslider.value

Print the value of avAudioPlayer.currentTime before assigning it to playslider.value
I think at the end it will become zero. and it will assign zero to playslider.value

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文