AVPlayer seekToTime not working properly
Asked Answered
W

2

6

Im having a problem with seeking with AVPlayer.seekToTime, I have the time index that I want to seek to inside a scrollViewDidScroll method like this:

func scrollViewDidScroll(scrollView: UIScrollView) {
    let offsetTime = scrollView.contentOffset.y * 0.1

    self.playerController.player?.seekToTime(CMTime(seconds: Double(offsetTime), preferredTimescale: 10), toleranceBefore: kCMTimePositiveInfinity, toleranceAfter: kCMTimeZero)

}

But the video does not flow nice. For example, when you scroll I want the video to only to move forward like 0.01 of a second (the video I have is real short only about 2.0 sec long) but when you scroll far enough, instead the video moves forward almost a whole second. Its really choppy and I'm not sure why I can't seek to like say 1.00 seconds to 1.01 seconds and have the image representing the time index on the player move. Is this possible? What am I doing wrong? Please help!

PS: I never call self.playerController.player?.play() if this helps

Wapentake answered 6/4, 2016 at 23:33 Comment(1)
try preferredTimescale:10000 and keep everything else the sameAugustina
U
20

Maybe your tolerance before is not set right. try the Following:

instead of your code:

  let offsetTime = scrollView.contentOffset.y * 0.1
  let seekTime : CMTime = CMTimeMake(Double(offsetTime), 1000)
self.playerController.player?.seekToTime(seekTime, toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero)
  • the Time Scale tells how many units you have per second
  • Also, toleranceBefore should ALSO be kCMTimeZero

hope this Helps :-)

Unwept answered 12/4, 2016 at 15:13 Comment(0)
R
1

Hey I tried the above code in a Xamarin project using Octane.Xam.Videoplayer: https://components.xamarin.com/view/video-player

It worked really well!

Here is my sample code which I put into a Custom Renderer that inherited from VideoPlayerRenderer which can be found in the namespace Octane.Xam.VideoPlayer.iOS.Renderers:

public void CustomSeek(int seekTime)
    {
        CMTime tm = new CMTime(seekTime, 10000);
        if(NativeVideoPlayer.Player != null)
            NativeVideoPlayer.Player.Seek(tm, CMTime.Zero, CMTime.Zero);
    }

Keep in mind that you will have to use Version 1.1.4 of the Octane.Xam.VideoPlayer to gain access to the NativeVideoPlayer property of the VideoPlayerRenderer. In the future this property may be renamed to PlayerControl.

Ricketts answered 9/9, 2016 at 16:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.