title: AVPlayer seekToTime does not play at correct position
body: <p>I have an AVPlayer which is playing a HLS video stream. My user interface provides a row of buttons, one for each "chapter" in the video (the buttons are labeled "1", "2", "3"). The app downloads some meta-data from a server which contains the list of chapter cut-in points denoted in seconds. For example, one video is 12 minutes in length - the list of chapter cut-in points are 0, 58, 71, 230, 530, etc., etc. </p>
  
  <p>When the user taps one of the "chapter buttons" the button handler code does this: </p>
  
  <pre><code>            [self.avPlayer pause];
  
      [self.avPlayer seekToTime: CMTimeMakeWithSeconds(seekTime, 600) 
                toleranceBefore: kCMTimeZero 
                 toleranceAfter: kCMTimeZero 
              completionHandler: ^(BOOL finished) 
              {
                  [self.avPlayer play];
              }];
  </code></pre>
  
  <p>Where "seekTime" is a local var which contains the cut-in point (as described above). </p>
  
  <p>The problem is that the video does not always start at the correct point. Sometimes it does. But sometimes it is anywhere from a tenth of a second, to 2 seconds BEFORE the requested seekTime. It NEVER starts after the requested seekTime. </p>
  
  <p>Here are some stats on the video encoding: </p>
  
  <p>Encoder: handbrakeCLI
  Codec: h.264
  Frame rate: 24 (actually, 23.976 - same as how it was shot)
  Video Bitrate: multiple bitrates (64/150/300/500/800/1200)
  Audio Bitrate: 128k
  Keyframes: 23.976 (1 per second)</p>
  
  <p>I am using the Apple mediafilesegmenter tool, of course, and the variantplaylistcreator to generate the playlist.</p>
  
  <p>The files are being served from an Amazon Cloud/S3 bucket. </p>
  
  <p>One area which I remain unclear about is CMTimeMakeWithSeconds - I have tried several variations based on different articles/docs I have read. For example, in the above excerpt I am using: </p>
  
  <p>CMTimeMakeWithSeconds(seekTime, 600)</p>
  
  <p>I have also tried: </p>
  
  <p>CMTimeMakeWithSeconds(seekTime, 1)</p>
  
  <p>I can't tell which is correct, though BOTH seem to produce the same inconsistent results! </p>
  
  <p>I have also tried: </p>
  
  <p>CMTimeMakeWithSeconds(seekTime, 23.967)</p>
  
  <p>Some articles claim this works like a numerator/denomenator, so n/1 should be correct where 'n' is number of seconds (as in CMTimeMakeWithseconds(n, 1)). But, the code was originally created by a different programmer (who is gone now) and he used the 600 number for the preferredTimeScale (ie. CMTimeMakeWithseconds(n, 600)).</p>
  
  <p>Can anyone offer any clues as to what I am doing wrong, or even if the kind of accuracy I am trying to achieve is even possible? </p>
  
  <p>And in case someone is tempted to offer "alternative" solutions, we are already considering breaking the video up into separate streams, one per chapter, but we do not believe that will give us the same performance in the sense that changing chapters will take longer as a new AVPlayerItem will have to be created and loaded, etc., etc., etc.  So if you think this is the only solution that will work (and we do expect this will achieve the result we want - ie. each chapter WILL start exactly where we want it to) feel free to say so.</p>
  
  <p>Thanks in advance! </p>