Skip to content

Commit

Permalink
fix(ios): call onLoadStart earlier (#3750)
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofMoch authored May 13, 2024
1 parent 9716f4c commit b3f08f6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
8 changes: 4 additions & 4 deletions examples/basic/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,7 @@ PODS:
- React-Mapbuffer (0.74.0):
- glog
- React-debug
- react-native-video (6.0.0-rc.0):
- react-native-video (6.0.0-rc.1):
- DoubleConversion
- glog
- hermes-engine
Expand All @@ -949,15 +949,15 @@ PODS:
- React-featureflags
- React-graphics
- React-ImageManager
- react-native-video/Video (= 6.0.0-rc.0)
- react-native-video/Video (= 6.0.0-rc.1)
- React-NativeModulesApple
- React-RCTFabric
- React-rendererdebug
- React-utils
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- Yoga
- react-native-video/Video (6.0.0-rc.0):
- react-native-video/Video (6.0.0-rc.1):
- DoubleConversion
- glog
- hermes-engine
Expand Down Expand Up @@ -1421,7 +1421,7 @@ SPEC CHECKSUMS:
React-jsitracing: 72dce571a387f9d085482142837222c31a8d6c3a
React-logger: 03f2f7b955cfe24593a2b8c9705c23e142d1ad24
React-Mapbuffer: 1ab3316cb736411bc641311b4b71e75099ae56ad
react-native-video: 3262598f55f8632e5d4dae90ba63d9d6e09bd563
react-native-video: 6bcf0049420848ecf85a46bc5ab3225022843fe6
React-nativeconfig: fef0a46effa630d72f137636a990d7df2f6a5f7c
React-NativeModulesApple: 8257dd73991f2e410b9c9d12801691439a0dc821
React-perflogger: 271f1111779fef70f9502d1d38da5132e5585230
Expand Down
39 changes: 26 additions & 13 deletions ios/Video/RCTVideo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
}
}

private var _isBuffering = false {
didSet {
onVideoBuffer?(["isBuffering": _isBuffering, "target": reactTag as Any])
}
}

/* IMA Ads */
private var _adTagUrl: String?
#if USE_GOOGLE_IMA
Expand Down Expand Up @@ -375,6 +381,17 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
throw NSError(domain: "", code: 0, userInfo: nil)
}

// Perform on next run loop, otherwise onVideoLoadStart is nil
onVideoLoadStart?([
"src": [
"uri": _source?.uri ?? NSNull(),
"type": _source?.type ?? NSNull(),
"isNetwork": NSNumber(value: _source?.isNetwork ?? false),
],
"drm": _drm?.json ?? NSNull(),
"target": reactTag,
])

if let uri = source.uri, uri.starts(with: "ph://") {
let photoAsset = await RCTVideoUtils.preparePHAsset(uri: uri)
return await playerItemPrepareText(asset: photoAsset, assetOptions: nil, uri: source.uri ?? "")
Expand Down Expand Up @@ -467,16 +484,6 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
_imaAdsManager.setUpAdsLoader()
}
#endif
// Perform on next run loop, otherwise onVideoLoadStart is nil
onVideoLoadStart?([
"src": [
"uri": _source?.uri ?? NSNull(),
"type": _source?.type ?? NSNull(),
"isNetwork": NSNumber(value: _source?.isNetwork ?? false),
],
"drm": _drm?.json ?? NSNull(),
"target": reactTag,
])
isSetSourceOngoing = false
applyNextSource()
}
Expand Down Expand Up @@ -1296,7 +1303,9 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
}

func handleReadyForDisplay(changeObject _: Any, change _: NSKeyValueObservedChange<Bool>) {
onVideoBuffer?(["isBuffering": false, "target": reactTag as Any])
if _isBuffering {
_isBuffering = false
}
onReadyForDisplay?([
"target": reactTag,
])
Expand Down Expand Up @@ -1430,12 +1439,16 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
}

func handlePlaybackBufferKeyEmpty(playerItem _: AVPlayerItem, change _: NSKeyValueObservedChange<Bool>) {
onVideoBuffer?(["isBuffering": true, "target": reactTag as Any])
if !_isBuffering {
_isBuffering = true
}
}

// Continue playing (or not if paused) after being paused due to hitting an unbuffered zone.
func handlePlaybackLikelyToKeepUp(playerItem _: AVPlayerItem, change _: NSKeyValueObservedChange<Bool>) {
onVideoBuffer?(["isBuffering": false, "target": reactTag as Any])
if _isBuffering {
_isBuffering = false
}
}

func handleTimeControlStatusChange(player: AVPlayer, change: NSKeyValueObservedChange<AVPlayer.TimeControlStatus>) {
Expand Down

0 comments on commit b3f08f6

Please sign in to comment.