Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implementation scaling mode dynamic android and ios #270

Merged
merged 9 commits into from
Oct 17, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.os.Looper
import android.util.Log
import android.view.ViewGroup.LayoutParams
import com.bitmovin.player.PlayerView
import com.bitmovin.player.api.ui.ScalingMode
import com.bitmovin.player.reactnative.converter.JsonConverter
import com.bitmovin.player.reactnative.extensions.getBooleanOrNull
import com.bitmovin.player.reactnative.extensions.getModule
Expand All @@ -31,6 +32,7 @@ class RNPlayerViewManager(private val context: ReactApplicationContext) : Simple
ATTACH_FULLSCREEN_BRIDGE("attachFullscreenBridge"),
SET_CUSTOM_MESSAGE_HANDLER_BRIDGE_ID("setCustomMessageHandlerBridgeId"),
SET_FULLSCREEN("setFullscreen"),
SET_SCALING_MODE("setScalingMode"),
}

/**
Expand Down Expand Up @@ -166,6 +168,11 @@ class RNPlayerViewManager(private val context: ReactApplicationContext) : Simple
setFullscreen(view, isFullscreen)
}
}
Commands.SET_SCALING_MODE -> {
args?.getString(1)?.let { scalingMode ->
setScalingMode(view, scalingMode)
}
}
}
}

Expand Down Expand Up @@ -196,6 +203,12 @@ class RNPlayerViewManager(private val context: ReactApplicationContext) : Simple
}
}

private fun setScalingMode(view: RNPlayerView, scalingMode: String) {
Handler(Looper.getMainLooper()).post {
view.playerView?.scalingMode = ScalingMode.valueOf(scalingMode)
}
}

private fun setCustomMessageHandlerBridgeId(view: RNPlayerView, customMessageHandlerBridgeId: NativeId) {
this.customMessageHandlerBridgeId = customMessageHandlerBridgeId
attachCustomMessageHandlerBridge(view)
Expand Down
1 change: 1 addition & 0 deletions ios/RNPlayerViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,6 @@ @interface RCT_EXTERN_REMAP_MODULE(NativePlayerView, RNPlayerViewManager, RCTVie
RCT_EXTERN_METHOD(attachFullscreenBridge:(nonnull NSNumber *)viewId fullscreenBridgeId:(NSString *)fullscreenBridgeId)
RCT_EXTERN_METHOD(setCustomMessageHandlerBridgeId:(nonnull NSNumber *)viewId customMessageHandlerBridgeId:(NSString *)fullscreenBridgeId)
RCT_EXTERN_METHOD(setFullscreen:(nonnull NSNumber *)viewId isFullscreen:(BOOL)isFullscreen)
RCT_EXTERN_METHOD(setScalingMode:(nonnull NSNumber *)viewId scalingMode:(NSString *)scalingMode)

@end
21 changes: 21 additions & 0 deletions ios/RNPlayerViewManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,27 @@ class RNPlayerViewManager: RCTViewManager {
}
}

@objc func setScalingMode(_ viewId: NSNumber, scalingMode: String) {
bridge.uiManager.addUIBlock { [weak self] _, views in
guard
let self,
let view = views?[viewId] as? RNPlayerView
else {
return
}
guard let playerView = view.playerView else {
return
}
if scalingMode == "Zoom" {
playerView.scalingMode = ScalingMode.zoom
} else if scalingMode == "Stretch" {
playerView.scalingMode = ScalingMode.stretch
} else {
playerView.scalingMode = ScalingMode.fit
}
jonathanm-tkf marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// Fetches the initialized `PlayerModule` instance on RN's bridge object.
private func getPlayerModule() -> PlayerModule? {
bridge.module(for: PlayerModule.self) as? PlayerModule
Expand Down
13 changes: 13 additions & 0 deletions src/components/PlayerView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { useProxy } from '../../hooks/useProxy';
import { FullscreenHandler, CustomMessageHandler } from '../../ui';
import { FullscreenHandlerBridge } from '../../ui/fullscreenhandlerbridge';
import { CustomMessageHandlerBridge } from '../../ui/custommessagehandlerbridge';
import { ScalingMode } from '../../styleConfig';
import { PictureInPictureConfig } from './pictureInPictureConfig';

/**
Expand Down Expand Up @@ -62,6 +63,10 @@ export interface PlayerViewProps extends BasePlayerViewProps, PlayerViewEvents {
* To use this property, a `FullscreenHandler` must be set.
*/
isFullscreenRequested?: Boolean;
/**
* The `Scaling Mode` that is used by the `PlayerView` to control the scaling mode.
jonathanm-tkf marked this conversation as resolved.
Show resolved Hide resolved
*/
scalingMode?: ScalingMode;
}

/**
Expand Down Expand Up @@ -100,6 +105,7 @@ export function PlayerView({
fullscreenHandler,
customMessageHandler,
isFullscreenRequested = false,
scalingMode = ScalingMode.Fit,
jonathanm-tkf marked this conversation as resolved.
Show resolved Hide resolved
pictureInPictureConfig,
...props
}: PlayerViewProps) {
Expand Down Expand Up @@ -179,6 +185,13 @@ export function PlayerView({
dispatch('setFullscreen', node, isFullscreenRequested);
}
}, [isFullscreenRequested, nativeView]);

useEffect(() => {
const node = findNodeHandle(nativeView.current);
if (node) {
dispatch('setScalingMode', node, scalingMode);
}
}, [scalingMode, nativeView]);
return (
<NativePlayerView
ref={nativeView}
Expand Down