Add onSeekComplete callaback to ios video player

This commit is contained in:
Kat Huang 2024-08-05 14:23:46 -06:00
parent 38aa2b057a
commit 18a0d234dd
2 changed files with 30 additions and 26 deletions

View File

@ -114,6 +114,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
@objc var onVideoProgress: RCTDirectEventBlock? @objc var onVideoProgress: RCTDirectEventBlock?
@objc var onVideoBandwidthUpdate: RCTDirectEventBlock? @objc var onVideoBandwidthUpdate: RCTDirectEventBlock?
@objc var onVideoSeek: RCTDirectEventBlock? @objc var onVideoSeek: RCTDirectEventBlock?
@objc var onVideoSeekComplete: RCTDirectEventBlock?
@objc var onVideoEnd: RCTDirectEventBlock? @objc var onVideoEnd: RCTDirectEventBlock?
@objc var onTimedMetadata: RCTDirectEventBlock? @objc var onTimedMetadata: RCTDirectEventBlock?
@objc var onVideoAudioBecomingNoisy: RCTDirectEventBlock? @objc var onVideoAudioBecomingNoisy: RCTDirectEventBlock?
@ -762,34 +763,36 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
} }
@objc @objc
func setSeek(_ time: NSNumber, _ tolerance: NSNumber) { func setSeek(_ info: NSDictionary!) {
let item: AVPlayerItem? = _player?.currentItem 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 _pendingSeek = true
_pendingSeekTime = seekTime.floatValue
guard item != nil, let player = _player, let item, item.status == AVPlayerItem.Status.readyToPlay else { return
_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
} }
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 @objc
func setRate(_ rate: Float) { func setRate(_ rate: Float) {

View File

@ -46,6 +46,7 @@ RCT_EXPORT_VIEW_PROPERTY(onVideoError, RCTDirectEventBlock);
RCT_EXPORT_VIEW_PROPERTY(onVideoProgress, RCTDirectEventBlock); RCT_EXPORT_VIEW_PROPERTY(onVideoProgress, RCTDirectEventBlock);
RCT_EXPORT_VIEW_PROPERTY(onVideoBandwidthUpdate, RCTDirectEventBlock); RCT_EXPORT_VIEW_PROPERTY(onVideoBandwidthUpdate, RCTDirectEventBlock);
RCT_EXPORT_VIEW_PROPERTY(onVideoSeek, RCTDirectEventBlock); RCT_EXPORT_VIEW_PROPERTY(onVideoSeek, RCTDirectEventBlock);
RCT_EXPORT_VIEW_PROPERTY(onVideoSeekComplete, RCTDirectEventBlock);
RCT_EXPORT_VIEW_PROPERTY(onVideoEnd, RCTDirectEventBlock); RCT_EXPORT_VIEW_PROPERTY(onVideoEnd, RCTDirectEventBlock);
RCT_EXPORT_VIEW_PROPERTY(onTimedMetadata, RCTDirectEventBlock); RCT_EXPORT_VIEW_PROPERTY(onTimedMetadata, RCTDirectEventBlock);
RCT_EXPORT_VIEW_PROPERTY(onVideoAudioBecomingNoisy, RCTDirectEventBlock); RCT_EXPORT_VIEW_PROPERTY(onVideoAudioBecomingNoisy, RCTDirectEventBlock);