AVPlayer seekToTime not working properly
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 :-)
AVPlayer seekToTime not working correctly
I tried to play your URL in Safari and it does play but says its live broadcast and does not allow your to seek, same with Chrome browser and I also tried to play it with my own AVPlayer on iPhone and it does not seek.
It simply mean that we can not seek live broadcast.
Swift AVPlayer seekToTime issue
For AVPlayer, there are different ways to let the play seeking time;
player.seekToTime(<#time: CMTime#CMTime#>)
for this method,player will seek to seekingTime quickly but not seeking the exactly where the seekingTime is, it means there will be some offset of the seekingTime.
Apple's Doc
Use this method to seek to a specified time for the current player item.
The time seeked to may differ from the specified time for efficiency.
For sample accurate seeking see seekToTime:toleranceBefore:toleranceAfter:.
player.seekToTime(<#T##time: CMTime##CMTime#>, completionHandler: <#T##(Bool) -> Void#>)
for this method,player will seek to seekingTime quickly and also,with the offset,you can do something with completionHandler
Apple's Doc
Use this method to seek to a specified time for the current player item and to be notified when the seek operation is complete.
The completion handler for any prior seek request that is still in process will be invoked immediately with the finished parameter
set to NO. If the new request completes without being interrupted by another seek request or by any other operation the specified
completion handler will be invoked with the finished parameter set to YES.
If you want to seeking more accurate, use the following method instead
player.seekToTime(<#T##time: CMTime##CMTime#>, toleranceBefore: <#T##CMTime#>, toleranceAfter: <#T##CMTime#>)
player.seekToTime(<#T##time: CMTime##CMTime#>, toleranceBefore: <#T##CMTime#>, toleranceAfter: <#T##CMTime#>, completionHandler: <#T##(Bool) -> Void#>)
this method will let the player seeking to specified time you asking,but will be slower(well..mostly depending on current media)
Apple's Doc
Use this method to seek to a specified time for the current player item.
The time seeked to will be within the range [time-toleranceBefore, time+toleranceAfter] and may differ from the specified time for efficiency.
Pass kCMTimeZero for both toleranceBefore and toleranceAfter to request sample accurate seeking which may incur additional decoding delay.
Messaging this method with beforeTolerance:kCMTimePositiveInfinity and afterTolerance:kCMTimePositiveInfinity is the same as messaging seekToTime: directly.
i've notice that current you are using
avPlayer?.currentItem?.seekToTime(CMTimeMakeWithSeconds(secondo, frameRate))
you can try to use this method to seeking the specified time
(slower of course)
avPlayer?.currentItem?.seekToTime(CMTimeMakeWithSeconds(secondo, frameRate), toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero)
Wish could help u out
AVPlayer seek not always accurate
Default tolerance value is infinity(when not specified), but usually real difference from the requested value is about a second - this allows seek to be fast.
If in your case it is jumping to the file start, it's probably because your file was not correctly encoded.
If you cannot change source of your files, the best you can do is specifying tolerance more than zero, but not too big. Like ±1 sec. The less tolerance you have the longer seek would perform, so try to find some balance.
Related Topics
Saving Coredata to a Web Server with Swift 3.0
How to Add a Show More/Show Less Uibutton to Control Uitextview
Swift: How to Flush Stdout After Println
Type of Expression Is Ambiguous Without More Context in Xcode 11
Mfmailcomposeviewcontroller Error [Mc] Filtering Mail Sheet Accounts for Bundle Id
Custom Mkannotation Not Moving When Coordinate Set
Swift - Getting Only Alphanumeric Characters from String
Modifying an Array Passed as an Argument to a Function in Swift
How to Apply a Grace Time Using Rx
Swift: Print Name of a Function Stored in a Variable
Argument of '#Selector' Does Not Refer to an '@Objc' Method, Property or Initializer
Cannot Load Module Coredata as Coredata
Trying to Add a Protocol to a Class Signature in Swift
Passing Dynamic Int Variable from One Class to Another Class in Swift
Avcapturevideopreviewlayer Is Not Visible on The Screenshot