1

I have implemented scrubbing using UISlider the problem is when the song is being played the slider doesnt initiate automatically ,it starts moving only when i tap on it and when the next song starts playing it moves automatically . i want the slider to move when the song starts playing ,this is the code which i am using and one more thing when i move the slider the song has to fast forward .I have added the code which i have written .can anyone help me out .Thanks

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

    }];

}

EDIT

can u explain where am i going wrong

-(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];
}
orgami
  • 161
  • 3
  • 15

1 Answers1

1

Your observer is being called when you tap on the uislider. This is why you have to tap on the slider first. Move the observer outside of the IBAction and possibly put it in the method that is run when the song starts.

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

}

Or your can look at this answer What gets called when a UISlider value changes? and setup the valueChange as an observer.

Community
  • 1
  • 1
Bot
  • 11,868
  • 11
  • 75
  • 131
  • @orgami what does your new code look like? Edit your original post. – Bot Aug 27 '12 at 17:01
  • Bot i edited my code , whati t does is .the slider keeps moving but the duration of the song doesnt increase and when i try to drag the slider the slider doesnt move – orgami Aug 27 '12 at 17:54
  • @orgami see update answer. This will resolve your original problem of the slider not updating. Your issue with not being able to drag the slider should be a different question. – Bot Aug 27 '12 at 18:18