滑块在播放音频结束时停止
在我的项目中,我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不能说这是否是一个错误,但尝试使用
AVAudioPlayer
的委托方法。在didFinishPlaying方法中将Slider值设置为maximumValue。这可能是解决方案之一。I cannot say whether this is a bug or not, But try using delegate methods of
AVAudioPlayer
. IndidFinishPlaying
method set Slider value to maximumValue. This could be one of the solution.在将 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