1

我已经使用 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];
}
4

1 回答 1

1

当您点击 uislider 时,您的观察者将被调用。这就是为什么您必须先点击滑块的原因。将观察者移出IBAction并可能将其放入歌曲开始时运行的方法中。

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

                                 }
                [self slider:nil];
                             }];
    [myPlayer play];

}

或者您可以查看此答案UISlider 值更改时会调用什么?并将 valueChange 设置为观察者。

于 2012-08-20T17:39:02.363 回答