我已经使用 UISlider 实现了擦洗,问题是在播放歌曲时滑块不会自动启动,只有当我点击它时它才会开始移动,并且当下一首歌曲开始播放时它会自动移动。我希望滑块在歌曲开始播放时移动,这是我正在使用的代码,当我移动滑块时,歌曲必须快进的另一件事。我已经添加了我写的代码。谁能帮助我出来。谢谢
-(IBAction)slider:(id)sender
{
CMTime interval = CMTimeMake(33, 1000);
self.playbackObserver = [myPlayer addPeriodicTimeObserverForInterval:interval queue:dispatch_get_current_queue() usingBlock:^(CMTime time) {
CMTime endTime = CMTimeConvertScale (myPlayer.currentItem.asset.duration, myPlayer.currentTime.timescale, kCMTimeRoundingMethod_RoundHalfAwayFromZero);
if (CMTimeCompare(endTime, kCMTimeZero) != 0) {
double normalizedTime = (double) myPlayer.currentTime.value / (double) endTime.value;
self.timeSlider.value = normalizedTime;
}
Float64 currentSeconds = CMTimeGetSeconds(myPlayer.currentTime);
int mins = currentSeconds/60.0;
int secs = fmodf(currentSeconds, 60.0);
NSString *minsString = mins < 10 ? [NSString stringWithFormat:@"0%d", mins] : [NSString stringWithFormat:@"%d", mins];
NSString *secsString = secs < 10 ? [NSString stringWithFormat:@"0%d", secs] : [NSString stringWithFormat:@"%d", secs];
currentTimeLabel.text = [NSString stringWithFormat:@"%@:%@", minsString, secsString];
}];
}
编辑
你能解释一下我哪里错了吗
-(IBAction)playButtonPressed:(UIButton *)sender
{
CMTime interval = CMTimeMake(33, 1000);
self.playbackObserver = [myPlayer addPeriodicTimeObserverForInterval:interval queue:dispatch_get_current_queue() usingBlock:^(CMTime time)
{
CMTime endTime = CMTimeConvertScale (myPlayer.currentItem.asset.duration, myPlayer.currentTime.timescale, kCMTimeRoundingMethod_RoundHalfAwayFromZero);
if (CMTimeCompare(endTime, kCMTimeZero) != 0) {
double normalizedTime = (double) myPlayer.currentTime.value / (double) endTime.value;
self.timeSlider.value = normalizedTime;
}
}];
[myPlayer play];
}
-(IBAction)slider:(id)sender
{
Float64 currentSeconds = CMTimeGetSeconds(myPlayer.currentTime);
int mins = currentSeconds/60.0;
int secs = fmodf(currentSeconds, 60.0);
NSString *minsString = mins < 10 ? [NSString stringWithFormat:@"0%d", mins] : [NSString stringWithFormat:@"%d", mins];
NSString *secsString = secs < 10 ? [NSString stringWithFormat:@"0%d", secs] : [NSString stringWithFormat:@"%d", secs];
currentTimeLabel.text = [NSString stringWithFormat:@"%@:%@", minsString, secsString];
}