Seek to a certain position in AVPlayer right after the asset was loaded

前端 未结 1 858
慢半拍i
慢半拍i 2020-12-08 10:56

This works: I have an AVPlayer which plays a video right after it was loaded. And this works fine.

This doesn\'t:

相关标签:
1条回答
  • 2020-12-08 11:29

    Couple of things which may help you:

    1. When checking if the player is ready to play, I check both the player itself and also the player's current item.

      if(self.player.status == AVPlayerStatusReadyToPlay &&
         self.player.currentItem.status == AVPlayerItemStatusReadyToPlay) {
          [self.player play];
      }
      
    2. It's possible that if you seek to e.g. 15 seconds, it may start from the beginning of the video instead if that is the location of the nearest keyframe. To force the player to seek to the exact location (note: this will take several seconds to do), do this:

      Float64 seconds = 500.0f;
      CMTime targetTime = CMTimeMakeWithSeconds(seconds, NSEC_PER_SEC);
      [self.player seekToTime:targetTime
       toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero];
      

      Or you can seek to the keyframe explicitly before/after the specified point by specifying one of the arguments as infinity:

      [self.player seekToTime:targetTime
       toleranceBefore:kCMTimePositiveInfinity toleranceAfter:kCMTimeZero];
      
    0 讨论(0)
提交回复
热议问题