如何实现uislider,以便它可以在AVQueueplayer中用作擦除器

时间:2012-08-20 16:47:37

标签: iphone objective-c ios xcode avqueueplayer

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

1 个答案:

答案 0 :(得分: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];

}

或者您可以查看此答案What gets called when a UISlider value changes?并将valueChange设置为观察者。