Convert from NStimeInterval to CMTime accurately

kmReddy picture kmReddy · Aug 13, 2013 · Viewed 8.7k times · Source

I am using two players one is MPMoviePlayerController and the other is AVPlayer. henever MPMoviePlayerController is seeked, it sets the time to AVPlayer. Everything is working fine. While converting MPMoviePlayerController current time to CMTime it is rounding off ex:(if mpplayer time is 11.80132 it is rounding to 11).

Without rounding off, how can we set the time to AVPlayer?

Code for getting mpplayer time and sending to AVPlayer class

[avplayerClass  setCurrentTime:[self.videoPlayer currentPlaybackTime]];
NSLog(@"CurrentTime:%f",[self.videoPlayer currentPlaybackTime]);//11.801345

Code in AVPlayer class and converting time to CMTime

-(void)setCurrentTime:(NSTimeInterval)seekTime
{
    CMTime seekingCM = CMTimeMake(seekTime, 1);
    [self.player seekToTime:seekingCM 
            toleranceBefore:kCMTimeZero 
             toleranceAfter:kCMTimePositiveInfinity];
    [self.player seekToTime:seekingCM];
    NSLog(@"Current time123ns%%%%%%:%f",CMTimeGetSeconds(seekingCM));//11
    NSLog(@"Current time123ns%%%%%%:%f",seekTime);//11.801345
}

Answer

Jenel Ejercito Myers picture Jenel Ejercito Myers · Jul 18, 2016

Try this :

  CMTime seekingCM = CMTimeMakeWithSeconds(playbackTime, 1000000);