Skip to content

Commit

Permalink
Merge pull request #10 from BrightcovePS/on-landscape-rotate
Browse files Browse the repository at this point in the history
Implemented the setFullscreen on rotation
  • Loading branch information
rafbc authored Jul 1, 2019
2 parents 2b21158 + 364b13b commit 349dc12
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 9 deletions.
13 changes: 11 additions & 2 deletions android/src/main/java/jp/manse/BrightcovePlayerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
public class BrightcovePlayerManager extends SimpleViewManager<BrightcovePlayerView> {
public static final String REACT_CLASS = "BrightcovePlayer";
public static final int COMMAND_SEEK_TO = 1;
public static final int COMMAND_SET_FULLSCREEN = 2;
public static final String EVENT_READY = "ready";
public static final String EVENT_PLAY = "play";
public static final String EVENT_PAUSE = "pause";
Expand Down Expand Up @@ -111,8 +112,8 @@ public void setFullscreen(BrightcovePlayerView view, boolean fullscreen) {
@Override
public Map<String, Integer> getCommandsMap() {
return MapBuilder.of(
"seekTo",
COMMAND_SEEK_TO
"seekTo", COMMAND_SEEK_TO,
"setFullscreen", COMMAND_SET_FULLSCREEN
);
}

Expand All @@ -125,6 +126,14 @@ public void receiveCommand(BrightcovePlayerView view, int commandType, @Nullable
view.seekTo((int)(args.getDouble(0) * 1000));
return;
}
case COMMAND_SET_FULLSCREEN: {
if (args.getBoolean(0)) {
view.dispatchEnterFullScreenClickEvent();
} else {
view.dispatchExitFullScreenClickEvent();
}
return;
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions android/src/main/java/jp/manse/BrightcovePlayerView.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,25 @@ public void processEvent(Event e) {
});
}

public void dispatchEnterFullScreenClickEvent() {
this.playerVideoView.getEventEmitter().emit(EventType.ENTER_FULL_SCREEN);
}

public void dispatchExitFullScreenClickEvent() {
this.playerVideoView.getEventEmitter().emit(EventType.EXIT_FULL_SCREEN);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
adjustMediaControllerDimensions();
}

private void adjustMediaControllerDimensions() {
mediaController.show();
mediaController.getBrightcoveControlBar().setVisibility(VISIBLE);
mediaController.getBrightcoveControlBar().setMinimumWidth(getWidth());
mediaController.getBrightcoveControlBar().setAlign(true);
}

public void setPolicyKey(String policyKey) {
Expand Down
11 changes: 11 additions & 0 deletions example/android/app/src/main/java/com/example/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.example;

import android.content.Intent;
import android.content.res.Configuration;

import com.facebook.react.ReactActivity;
import com.facebook.react.ReactActivityDelegate;
import com.facebook.react.ReactRootView;
Expand All @@ -25,4 +28,12 @@ protected ReactRootView createRootView() {
}
};
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Intent intent = new Intent("onConfigurationChanged");
intent.putExtra("newConfig", newConfig);
this.sendBroadcast(intent);
}
}
56 changes: 50 additions & 6 deletions example/components/BCPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,17 @@ import Orientation from 'react-native-orientation';

export default class BCPlayer extends Component {

state = {
orientation: null
constructor(props) {
super(props);

this.state = {
orientation: null,
forcedOrientation: false
}

this.orientationDidChange = this.orientationDidChange.bind(this);
this.onBeforeEnterFullscreen = this.onBeforeEnterFullscreen.bind(this);
this.onBeforeExitFullscreen = this.onBeforeExitFullscreen.bind(this);
}

componentWillMount() {
Expand All @@ -18,27 +27,62 @@ export default class BCPlayer extends Component {

const initial = Orientation.getInitialOrientation();
this.setState({ orientation: initial });
// Remember to remove listener
Orientation.removeOrientationListener(this.orientationDidChange);
}

componentDidMount() {
Orientation.addOrientationListener(this.orientationDidChange);
}

onBeforeEnterFullscreen() {
Orientation.lockToLandscape();

if (this.state.orientation === 'PORTRAIT') {
this.setState({ forcedOrientation: true });
Orientation.lockToLandscape();
}

this.props.onBeforeEnterFullscreen && this.props.onBeforeEnterFullscreen();
}

onBeforeExitFullscreen() {
this.setState({ forcedOrientation: false });

Orientation.lockToPortrait();
Orientation.unlockAllOrientations();

this.props.onBeforeExitFullscreen && this.props.onBeforeExitFullscreen();
}

orientationDidChange(orientation) {
console.log("orientation did change");
// If the player hasn't been loaded yet, then don't do anything
if (!this.player) return;

switch (orientation) {
case 'LANDSCAPE':
// Only set the fullscreen in this case, if the forced orientation by the "lockTolandscape" hasn't been called
// otherwise, if you call the setfullscreen twice, it might be buggy
if (!this.state.forcedOrientation) {
this.player.setFullscreen(true);
}
break;
case 'PORTRAIT':
this.player.setFullscreen(false);
break;
}

this.setState({ orientation });
}

render() {
return (<BrightcovePlayer
ref={(player) => this.player = player}
{...this.props}
style={[styles.player, this.props.style]}
onBeforeEnterFullscreen={this.onBeforeEnterFullscreen.bind(this)}
onBeforeExitFullscreen={this.onBeforeExitFullscreen.bind(this)}
onBeforeEnterFullscreen={this.onBeforeEnterFullscreen}
onBeforeExitFullscreen={this.onBeforeExitFullscreen}
/>

);
}
}
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export class BrightcovePlayer extends React.Component<
{}
> {
seekTo(position: number): {};
setFullscreen(fullscreen: boolean): {};
}

export type BrightcovePlayerPosterProps = {
Expand Down
1 change: 1 addition & 0 deletions ios/BrightcovePlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
@property (nonatomic, copy) RCTDirectEventBlock onExitFullscreen;

-(void) seekTo:(NSNumber *)time;
-(void) setFullscreen:(BOOL *)fullscreen;
-(void)dispose;

@end
9 changes: 9 additions & 0 deletions ios/BrightcovePlayerManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ - (dispatch_queue_t)methodQueue {
}];
}

RCT_EXPORT_METHOD(setFullscreen:(nonnull NSNumber *)reactTag fullscreen:(BOOL)fullscreen) {
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
BrightcovePlayer *player = (BrightcovePlayer*)viewRegistry[reactTag];
if ([player isKindOfClass:[BrightcovePlayer class]]) {
[player setFullscreen:fullscreen];
}
}];
}

RCT_EXPORT_METHOD(dispose:(nonnull NSNumber *)reactTag) {
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
BrightcovePlayer *player = (BrightcovePlayer*)viewRegistry[reactTag];
Expand Down
18 changes: 17 additions & 1 deletion src/BrightcovePlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,28 @@ BrightcovePlayer.prototype.seekTo = Platform.select({
android: function(seconds) {
UIManager.dispatchViewManagerCommand(
ReactNative.findNodeHandle(this._root),
UIManager.BrightcovePlayer.Commands.seekTo,
UIManager.getViewManagerConfig('BrightcovePlayer').Commands.seekTo,
[seconds]
);
}
});

BrightcovePlayer.prototype.setFullscreen = Platform.select({
ios: function(fullscreen) {
NativeModules.BrightcovePlayerManager.setFullscreen(
ReactNative.findNodeHandle(this),
fullscreen
);
},
android: function(fullscreen) {
UIManager.dispatchViewManagerCommand(
ReactNative.findNodeHandle(this._root),
UIManager.getViewManagerConfig('BrightcovePlayer').Commands.setFullscreen,
[fullscreen]
);
}
});

BrightcovePlayer.propTypes = {
...(ViewPropTypes || View.propTypes),
policyKey: PropTypes.string,
Expand Down

0 comments on commit 349dc12

Please sign in to comment.