Skip to content

Commit

Permalink
Merge pull request #270 from take0ffmedia/feature/scalingMode
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandkakonyi authored Oct 17, 2023
2 parents 5d6e0f5 + efa4217 commit cc9b11a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
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 @@ -68,5 +68,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
24 changes: 24 additions & 0 deletions ios/RNPlayerViewManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,30 @@ 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
}
switch scalingMode {
case "Zoom":
playerView.scalingMode = .zoom
case "Stretch":
playerView.scalingMode = .stretch
case "Fit":
playerView.scalingMode = .fit
default:
break
}
}
}

/// Fetches the initialized `PlayerModule` instance on RN's bridge object.
private func getPlayerModule() -> PlayerModule? {
bridge.module(for: PlayerModule.self) as? PlayerModule
Expand Down
14 changes: 14 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,11 @@ export interface PlayerViewProps extends BasePlayerViewProps, PlayerViewEvents {
* To use this property, a `FullscreenHandler` must be set.
*/
isFullscreenRequested?: Boolean;
/**
* A value defining how the video is displayed within the parent container's bounds.
* Possible values are defined in `ScalingMode`.
*/
scalingMode?: ScalingMode;
}

/**
Expand Down Expand Up @@ -100,6 +106,7 @@ export function PlayerView({
fullscreenHandler,
customMessageHandler,
isFullscreenRequested = false,
scalingMode,
pictureInPictureConfig,
...props
}: PlayerViewProps) {
Expand Down Expand Up @@ -179,6 +186,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

0 comments on commit cc9b11a

Please sign in to comment.