Pat
Pat

Reputation: 649

AVPlayer seekTo not accurate

So I have an HLS stream that I have AVPlayer playing. I am trying to create a button to jump back 30 seconds like so:

- (void) rewindStream
{
    NSLog(@"Seeking...");
    NSLog(@"Current Time: %f", CMTimeGetSeconds(self.player.currentTime));
    NSLog(@"New Time: %f", CMTimeGetSeconds(CMTimeMakeWithSeconds(CMTimeGetSeconds(self.player.currentTime) - 30.0f, self.player.currentTime.timescale)));
    [self.player pause];
    CMTime cmTime = CMTimeMakeWithSeconds(CMTimeGetSeconds(self.player.currentTime) - 30.0f, self.player.currentTime.timescale);
    [self.player seekToTime:cmTime toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero completionHandler:^(BOOL finished) {
        NSLog(@"Complete. Current Time: %f", CMTimeGetSeconds(self.player.currentTime));
        [self.player play];
    }];
}

Which works sometimes, but not others. Here is the log:

Seeking...
Current Time: 47.253361
New Time: 37.254001
Complete. Current Time: 37.254944
Seeking...
Current Time: 59.409800
New Time: 49.410447
Complete. Current Time: 50.103244
Seeking...
Current Time: 68.780054
New Time: 58.780436
Complete. Current Time: 60.086244
Seeking...
Current Time: 80.493733
New Time: 70.494375
Complete. Current Time: 80.140578
Seeking...
Current Time: 92.674773
New Time: 82.675062
Complete. Current Time: 110.135244

I have the tolerance set, and I am making sure I give enough time for the segments to download/buffer so that seeking is possible, but I am not sure what the issue could be.

Any help would be greatly appreciated.

Thanks.

Upvotes: 3

Views: 2546

Answers (2)

amergin
amergin

Reputation: 3176

I always use a timescale of 60000 which gives enough accuracy for almost anything. Not sure if this is your issue but I've found that most of my CMTime problems came before I settled on this. Warren Moore has a great post describing this at http://warrenmoore.net/understanding-cmtime

Upvotes: 4

David Pullar
David Pullar

Reputation: 706

It looks right, off the top of my head, maybe something like this?

CMTime currentTime = self.player.currentTime;
CMTime timeToSubtract = CMTimeMakeWithSeconds(30, 1);

CMTime resultTime = CMTimeSubtract(currentTime, timeToSubtract);

[self.player seekToTime:resultTime];

Upvotes: 0

Related Questions