问题
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
回答1:
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 :-)
回答2:
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.
来源:https://stackoverflow.com/questions/36464127/avplayer-seektotime-not-working-properly