16

this should be a simple one. I have an AVPlayer playing a video file and I want to be able to jump to a specific time but I'm having some trouble understanding how CMTime works.

I need to specify the time in seconds. For example: if I wanted to jump to second 10.8 I'd like to do something like this:

[self.avPlayer.currentItem seekToTime:CMTimeMakeWithSeconds(10.8, 1];

But I'm not getting the result I want.

4 Answers 4

25

http://warrenmoore.net/understanding-cmtime

Might give you a clearer idea of CMTime.

If you changed your line to read...

[self.avPlayer.currentItem seekToTime:CMTimeMakeWithSeconds(10.8, 60000)];

You might get better results. The 60000 is how many items make up a single second. If you use 1 for this value then you can only jump to a single place per second - by putting 60000 you are giving yourself 60000 places between second 10 and second 11.

2
14

In case you need to seek to a very specific time I would recommend using this method with tolerance:

let myTime = CMTime(seconds: 10.8, preferredTimescale: 60000)    
player.seek(to: myTime, toleranceBefore: .zero, toleranceAfter: .zero)

Documentation: link. Apple suggests using it for high accuracy.

2
  • 1
    after several solutions on this post, I ended needing to use the toleranceBefore and toleranceAfter parameters for the seek to work properly.
    – Nick N
    Mar 4, 2021 at 3:50
  • there's no ".zero" in CMTime
    – Fattie
    Apr 1, 2023 at 23:57
3

Swift version

avPlayer.seek(to:CMTimeMakeWithSeconds(10.8,1000))
1
  • 1
    Swift 5.1 version
    – Asad Jamil
    Jan 21, 2020 at 10:24
1

Swift 5.1 version

player?.seek(to: CMTime(value: CMTimeValue(10.8), timescale: 1000))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.