AVPlayer seekToTime does not play at correct position

前端 未结 5 1182
忘了有多久
忘了有多久 2021-01-31 03:31

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\",

相关标签:
5条回答
  • 2021-01-31 04:01

    please use function like [player seekToTime:CMTimeMakeWithSeconds(seekTime,1)] .
    Because your tolerance value kCMTimeZero will take more time to seek.Instead of using tolerance value of kCMTimeZero you can use kCMTimeIndefinite which is equivalent the function that i specified earlier.

    0 讨论(0)
  • 2021-01-31 04:06

    Put this code it may be resolve your problem.

    let targetTime = CMTimeMakeWithSeconds(videoLastDuration, 1) // videoLastDuration hold the previous video state.
    self.playerController.player?.currentItem?.seekToTime(targetTime, toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero)
    
    0 讨论(0)
  • 2021-01-31 04:09

    My suggestion: 1) Don't use [avplayer seekToTime: toleranceBefore: toleranceAfter: ], this will delay your seek time 4-5 seconds.

    2) HLS video cut to 10 seconds per segment. Your chapter start postion should fit the value which is multipes of 10. As the segment starts with I frame, on this way, you can get quick seek time and accurate time.

    0 讨论(0)
  • 2021-01-31 04:13
    int32_t timeScale = self.player.currentItem.asset.duration.timescale;
    CMTime time = CMTimeMakeWithSeconds(77.000000, timeScale);
    [self.player seekToTime:time toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero];
    

    I had a problem with 'seekToTime'. I solved my problem with this code. 'timescale' is trick for this problem.

    Swift version:

    let playerTimescale = self.player.currentItem?.asset.duration.timescale ?? 1
    let time =  CMTime(seconds: 77.000000, preferredTimescale: playerTimescale)
    self.player.seek(to: time, toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero) { (finished) in /* Add your completion code here */
    }
    
    0 讨论(0)
  • 2021-01-31 04:21

    Swift5

    let seconds = 45.0
    let time = CMTimeMake(value: seconds, timescale: 1)
    player?.seek(to: time, toleranceBefore: CMTime.zero, toleranceAfter: CMTime.zero)
    
    0 讨论(0)
提交回复
热议问题