From 3cd7ab60b27e8820607bd83a5681bc7ba7e6c52e Mon Sep 17 00:00:00 2001 From: Seyed Mostafa Hasani Date: Mon, 20 May 2024 13:51:48 +0330 Subject: [PATCH] feat: add setVolume function to component's ref (#3794) * feat: add setVolume function to component's ref * Update methods.mdx --- .../main/java/com/brentvatne/react/VideoManagerModule.kt | 7 +++++++ docs/pages/component/methods.mdx | 8 ++++++++ ios/Video/RCTVideoManager.m | 2 ++ ios/Video/RCTVideoManager.swift | 7 +++++++ src/Video.tsx | 7 +++++++ src/specs/VideoNativeComponent.ts | 1 + 6 files changed, 32 insertions(+) diff --git a/android/src/main/java/com/brentvatne/react/VideoManagerModule.kt b/android/src/main/java/com/brentvatne/react/VideoManagerModule.kt index 69f028dc..11781aa1 100644 --- a/android/src/main/java/com/brentvatne/react/VideoManagerModule.kt +++ b/android/src/main/java/com/brentvatne/react/VideoManagerModule.kt @@ -54,6 +54,13 @@ class VideoManagerModule(reactContext: ReactApplicationContext?) : ReactContextB } } + @ReactMethod + fun setVolume(volume: Float, reactTag: Int) { + performOnPlayerView(reactTag) { + it?.setVolumeModifier(volume) + } + } + companion object { private const val REACT_CLASS = "VideoManager" } diff --git a/docs/pages/component/methods.mdx b/docs/pages/component/methods.mdx index e68e0c5b..9c11e600 100644 --- a/docs/pages/component/methods.mdx +++ b/docs/pages/component/methods.mdx @@ -92,6 +92,14 @@ By default iOS seeks within 100 milliseconds of the target position. If you need tolerance is the max distance in milliseconds from the seconds position that's allowed. Using a more exact tolerance can cause seeks to take longer. If you want to seek exactly, set tolerance to 0. +### `setVolume` + + + +`setVolume(value): Promise` + +This function will change the volume exactly like [volume](../props#volume) property. default value and range are the same then. + ### Example Usage ```tsx diff --git a/ios/Video/RCTVideoManager.m b/ios/Video/RCTVideoManager.m index 3ea9e025..d0ead34a 100644 --- a/ios/Video/RCTVideoManager.m +++ b/ios/Video/RCTVideoManager.m @@ -86,4 +86,6 @@ RCT_EXTERN_METHOD(presentFullscreenPlayer : (nonnull NSNumber*)reactTag) RCT_EXTERN_METHOD(dismissFullscreenPlayer : (nonnull NSNumber*)reactTag) +RCT_EXTERN_METHOD(setVolume : (nonnull float*)volume reactTag : (nonnull NSNumber*)reactTag) + @end diff --git a/ios/Video/RCTVideoManager.swift b/ios/Video/RCTVideoManager.swift index 94dfcc4c..d8053f37 100644 --- a/ios/Video/RCTVideoManager.swift +++ b/ios/Video/RCTVideoManager.swift @@ -77,6 +77,13 @@ class RCTVideoManager: RCTViewManager { }) } + @objc(setVolume:reactTag:) + func setVolume(value: Float, reactTag: NSNumber) { + performOnVideoView(withReactTag: reactTag, callback: { videoView in + videoView?.setVolume(value) + }) + } + override class func requiresMainQueueSetup() -> Bool { return true } diff --git a/src/Video.tsx b/src/Video.tsx index e871964e..364c15b1 100644 --- a/src/Video.tsx +++ b/src/Video.tsx @@ -64,6 +64,7 @@ export interface VideoRef { restore: boolean, ) => void; save: (options: object) => Promise; + setVolume: (volume: number) => void; } const Video = forwardRef( @@ -289,6 +290,10 @@ const Video = forwardRef( [setRestoreUserInterfaceForPIPStopCompletionHandler], ); + const setVolume = useCallback((volume: number) => { + return VideoManager.setVolume(volume, getReactTag(nativeRef)); + }, []); + const onVideoLoadStart = useCallback( (e: NativeSyntheticEvent) => { hasPoster && setShowPoster(true); @@ -505,6 +510,7 @@ const Video = forwardRef( pause, resume, restoreUserInterfaceForPictureInPictureStopCompleted, + setVolume, }), [ seek, @@ -514,6 +520,7 @@ const Video = forwardRef( pause, resume, restoreUserInterfaceForPictureInPictureStopCompleted, + setVolume, ], ); diff --git a/src/specs/VideoNativeComponent.ts b/src/specs/VideoNativeComponent.ts index 1b383eae..4f1c738f 100644 --- a/src/specs/VideoNativeComponent.ts +++ b/src/specs/VideoNativeComponent.ts @@ -372,6 +372,7 @@ export interface VideoManagerType { licenseUrl: string, reactTag: number, ) => Promise; + setVolume: (volume: number, reactTag: number) => Promise; } export interface VideoDecoderPropertiesType {