Question

I'm making an app that uses AVPlayer. In one of my views I have a UISlider with which the user should be able to scrub forward and backward. I'm having some trouble getting seekToTime to work as I want. When I try to change time the playback starts from 0, and I'm not sure how to solve this. My current implementation looks like this:

[self.progressSlider  addTarget:self action:@selector(sliderValueChanged) forControlEvents:UIControlEventValueChanged];

[self.progressSlider  addTarget:self action:@selector(sliderReleased) forControlEvents:UIControlEventTouchUpInside];


- (void)sliderValueChanged {

    [...]
    [player pause];

}

- (void)sliderReleased {

    float timeInSecond = self.progressSlider.value;

    timeInSecond *= 1000;

    // I have trie to hard code this value, but I get the same result.
    CMTime cmTime = CMTimeMake(timeInSecond, NSEC_PER_SEC);

    [player.currentItem seekToTime:cmTime toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero completionHandler:^(BOOL finished) {

        if (finished) {
            [player play];
        }
    }];
}

Any ideas on what I'm doing wrong?

Can mention that I stream audio using AVPlayer's: playerItemWithURL if that makes any difference.

Was it helpful?

Solution

One problem is that NSEC_PER_SEC is 1,000,000,000, so CMTimeMake(timeInSecond, NSEC_PER_SEC) is going to be a really tiny number (unless timeInSecond is ridiculously huge) - in fact, it will be arbitrarily close to zero, which is exactly what you are experiencing.

Just to be clear: CMTimeMake defines a rational number. You are giving a numerator and a denominator. If your denominator is huge, the rational number will be tiny.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top