diff --git a/ios/Video/RCTVideo.swift b/ios/Video/RCTVideo.swift index 325a06f1..3961b259 100644 --- a/ios/Video/RCTVideo.swift +++ b/ios/Video/RCTVideo.swift @@ -114,6 +114,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH @objc var onVideoProgress: RCTDirectEventBlock? @objc var onVideoBandwidthUpdate: RCTDirectEventBlock? @objc var onVideoSeek: RCTDirectEventBlock? + @objc var onVideoSeekComplete: RCTDirectEventBlock? @objc var onVideoEnd: RCTDirectEventBlock? @objc var onTimedMetadata: RCTDirectEventBlock? @objc var onVideoAudioBecomingNoisy: RCTDirectEventBlock? @@ -762,34 +763,36 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH } @objc - func setSeek(_ time: NSNumber, _ tolerance: NSNumber) { - let item: AVPlayerItem? = _player?.currentItem - +func setSeek(_ info: NSDictionary!) { + let seekTime: NSNumber! = info["time"] as! NSNumber + let seekTolerance: NSNumber! = info["tolerance"] as! NSNumber + let item: AVPlayerItem? = _player?.currentItem + guard let player = _player, let item = item, item.status == AVPlayerItem.Status.readyToPlay else { _pendingSeek = true - - guard item != nil, let player = _player, let item, item.status == AVPlayerItem.Status.readyToPlay else { - _pendingSeekTime = time.floatValue - return - } - - RCTPlayerOperations.seek( - player: player, - playerItem: item, - paused: _paused, - seekTime: time.floatValue, - seekTolerance: tolerance.floatValue - ) { [weak self] (_: Bool) in - guard let self else { return } - - self._playerObserver.addTimeObserverIfNotSet() - self.setPaused(self._paused) - self.onVideoSeek?(["currentTime": NSNumber(value: Float(CMTimeGetSeconds(item.currentTime()))), - "seekTime": time, - "target": self.reactTag]) - } - - _pendingSeek = false + _pendingSeekTime = seekTime.floatValue + return } + let wasPaused = _paused + + let cmSeekTime = CMTimeMakeWithSeconds(Float64(seekTime.floatValue), preferredTimescale: Int32(NSEC_PER_SEC)) + let tolerance = CMTimeMakeWithSeconds(Float64(seekTolerance.floatValue), preferredTimescale: Int32(NSEC_PER_SEC)) + + player.seek(to: cmSeekTime, toleranceBefore: tolerance, toleranceAfter: tolerance) { [weak self] (finished) in + guard let self = self, finished else { return } + + self._playerObserver.addTimeObserverIfNotSet() + if !wasPaused { + self.setPaused(false) + } + + let currentTime = NSNumber(value: Float(CMTimeGetSeconds(item.currentTime()))) + self.onVideoSeekComplete?(["currentTime": currentTime, + "seekTime": seekTime, + "target": self.reactTag]) + } + + _pendingSeek = false +} @objc func setRate(_ rate: Float) { diff --git a/ios/Video/RCTVideoManager.m b/ios/Video/RCTVideoManager.m index 3807f955..49a54933 100644 --- a/ios/Video/RCTVideoManager.m +++ b/ios/Video/RCTVideoManager.m @@ -46,6 +46,7 @@ RCT_EXPORT_VIEW_PROPERTY(onVideoError, RCTDirectEventBlock); RCT_EXPORT_VIEW_PROPERTY(onVideoProgress, RCTDirectEventBlock); RCT_EXPORT_VIEW_PROPERTY(onVideoBandwidthUpdate, RCTDirectEventBlock); RCT_EXPORT_VIEW_PROPERTY(onVideoSeek, RCTDirectEventBlock); +RCT_EXPORT_VIEW_PROPERTY(onVideoSeekComplete, RCTDirectEventBlock); RCT_EXPORT_VIEW_PROPERTY(onVideoEnd, RCTDirectEventBlock); RCT_EXPORT_VIEW_PROPERTY(onTimedMetadata, RCTDirectEventBlock); RCT_EXPORT_VIEW_PROPERTY(onVideoAudioBecomingNoisy, RCTDirectEventBlock);