feat: add getCurrentPosition to component's ref (#3824)
* feat: add getCurrentPosition to component's ref --------- Co-authored-by: mostafahasani <hasanie.mostafa@alopeyk.com>
This commit is contained in:
parent
e23e02b359
commit
c7f4d7b83b
@ -124,6 +124,7 @@ import com.brentvatne.react.R;
|
||||
import com.brentvatne.receiver.AudioBecomingNoisyReceiver;
|
||||
import com.brentvatne.receiver.BecomingNoisyListener;
|
||||
import com.facebook.react.bridge.LifecycleEventListener;
|
||||
import com.facebook.react.bridge.Promise;
|
||||
import com.facebook.react.bridge.UiThreadUtil;
|
||||
import com.facebook.react.uimanager.ThemedReactContext;
|
||||
import com.google.ads.interactivemedia.v3.api.AdError;
|
||||
@ -697,6 +698,15 @@ public class ReactExoplayerView extends FrameLayout implements
|
||||
mainHandler.postDelayed(mainRunnable, 1);
|
||||
}
|
||||
|
||||
public void getCurrentPosition(Promise promise) {
|
||||
if (player != null) {
|
||||
double currentPosition = player.getCurrentPosition() / 1000;
|
||||
promise.resolve(currentPosition);
|
||||
} else {
|
||||
promise.reject("PLAYER_NOT_AVAILABLE", "Player is not initialized.");
|
||||
}
|
||||
}
|
||||
|
||||
private void initializePlayerCore(ReactExoplayerView self) {
|
||||
ExoTrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory();
|
||||
self.trackSelector = new DefaultTrackSelector(getContext(), videoTrackSelectionFactory);
|
||||
|
@ -2,6 +2,7 @@ package com.brentvatne.react
|
||||
|
||||
import com.brentvatne.common.toolbox.ReactBridgeUtils
|
||||
import com.brentvatne.exoplayer.ReactExoplayerView
|
||||
import com.facebook.react.bridge.Promise
|
||||
import com.facebook.react.bridge.ReactApplicationContext
|
||||
import com.facebook.react.bridge.ReactContextBaseJavaModule
|
||||
import com.facebook.react.bridge.ReactMethod
|
||||
@ -61,6 +62,13 @@ class VideoManagerModule(reactContext: ReactApplicationContext?) : ReactContextB
|
||||
}
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
fun getCurrentPosition(reactTag: Int, promise: Promise) {
|
||||
performOnPlayerView(reactTag) {
|
||||
it?.getCurrentPosition(promise)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val REACT_CLASS = "VideoManager"
|
||||
}
|
||||
|
@ -100,6 +100,15 @@ tolerance is the max distance in milliseconds from the seconds position that's a
|
||||
|
||||
This function will change the volume exactly like [volume](./props#volume) property. default value and range are the same then.
|
||||
|
||||
### `getCurrentPosition`
|
||||
|
||||
<PlatformsList types={['Android', 'iOS']} />
|
||||
|
||||
`getCurrentPosition(): Promise<number>`
|
||||
|
||||
This function retrieves and returns the precise current position of the video playback, measured in seconds.
|
||||
This function will throw an error if player is not initialized.
|
||||
|
||||
### Example Usage
|
||||
|
||||
```tsx
|
||||
|
@ -1622,6 +1622,16 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
|
||||
}
|
||||
}
|
||||
|
||||
@objc
|
||||
func getCurrentPlaybackTime(_ resolve: @escaping RCTPromiseResolveBlock, _ reject: @escaping RCTPromiseRejectBlock) {
|
||||
if let player = _playerItem {
|
||||
let currentTime = RCTVideoUtils.getCurrentTime(playerItem: player)
|
||||
resolve(currentTime)
|
||||
} else {
|
||||
reject("PLAYER_NOT_AVAILABLE", "Player is not initialized.", nil)
|
||||
}
|
||||
}
|
||||
|
||||
// Workaround for #3418 - https://github.com/TheWidlarzGroup/react-native-video/issues/3418#issuecomment-2043508862
|
||||
@objc
|
||||
func setOnClick(_: Any) {}
|
||||
|
@ -88,4 +88,8 @@ RCT_EXTERN_METHOD(dismissFullscreenPlayer : (nonnull NSNumber*)reactTag)
|
||||
|
||||
RCT_EXTERN_METHOD(setVolume : (nonnull float*)volume reactTag : (nonnull NSNumber*)reactTag)
|
||||
|
||||
RCT_EXTERN_METHOD(getCurrentPosition
|
||||
: (nonnull NSNumber*)reactTag resolver
|
||||
: (RCTPromiseResolveBlock)resolve rejecter
|
||||
: (RCTPromiseRejectBlock)reject)
|
||||
@end
|
||||
|
@ -86,6 +86,13 @@ class RCTVideoManager: RCTViewManager {
|
||||
})
|
||||
}
|
||||
|
||||
@objc(getCurrentPosition:resolver:rejecter:)
|
||||
func getCurrentPosition(reactTag: NSNumber, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
||||
performOnVideoView(withReactTag: reactTag, callback: { videoView in
|
||||
videoView?.getCurrentPlaybackTime(resolve, reject)
|
||||
})
|
||||
}
|
||||
|
||||
override class func requiresMainQueueSetup() -> Bool {
|
||||
return true
|
||||
}
|
||||
|
@ -65,6 +65,7 @@ export interface VideoRef {
|
||||
) => void;
|
||||
save: (options: object) => Promise<VideoSaveData>;
|
||||
setVolume: (volume: number) => void;
|
||||
getCurrentPosition: () => Promise<number>;
|
||||
}
|
||||
|
||||
const Video = forwardRef<VideoRef, ReactVideoProps>(
|
||||
@ -295,6 +296,10 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
|
||||
return VideoManager.setVolume(volume, getReactTag(nativeRef));
|
||||
}, []);
|
||||
|
||||
const getCurrentPosition = useCallback(() => {
|
||||
return VideoManager.getCurrentPosition(getReactTag(nativeRef));
|
||||
}, []);
|
||||
|
||||
const onVideoLoadStart = useCallback(
|
||||
(e: NativeSyntheticEvent<OnLoadStartData>) => {
|
||||
hasPoster && setShowPoster(true);
|
||||
@ -512,6 +517,7 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
|
||||
resume,
|
||||
restoreUserInterfaceForPictureInPictureStopCompleted,
|
||||
setVolume,
|
||||
getCurrentPosition,
|
||||
}),
|
||||
[
|
||||
seek,
|
||||
@ -522,6 +528,7 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
|
||||
resume,
|
||||
restoreUserInterfaceForPictureInPictureStopCompleted,
|
||||
setVolume,
|
||||
getCurrentPosition,
|
||||
],
|
||||
);
|
||||
|
||||
|
@ -389,6 +389,7 @@ export interface VideoManagerType {
|
||||
reactTag: number,
|
||||
) => Promise<void>;
|
||||
setVolume: (volume: number, reactTag: number) => Promise<void>;
|
||||
getCurrentPosition: (reactTag: number) => Promise<number>;
|
||||
}
|
||||
|
||||
export interface VideoDecoderPropertiesType {
|
||||
|
Loading…
Reference in New Issue
Block a user