Skip to content

Commit 349dc12

Browse files
authored
Merge pull request #10 from BrightcovePS/on-landscape-rotate
Implemented the setFullscreen on rotation
2 parents 2b21158 + 364b13b commit 349dc12

File tree

8 files changed

+113
-9
lines changed

8 files changed

+113
-9
lines changed

android/src/main/java/jp/manse/BrightcovePlayerManager.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
public class BrightcovePlayerManager extends SimpleViewManager<BrightcovePlayerView> {
1818
public static final String REACT_CLASS = "BrightcovePlayer";
1919
public static final int COMMAND_SEEK_TO = 1;
20+
public static final int COMMAND_SET_FULLSCREEN = 2;
2021
public static final String EVENT_READY = "ready";
2122
public static final String EVENT_PLAY = "play";
2223
public static final String EVENT_PAUSE = "pause";
@@ -111,8 +112,8 @@ public void setFullscreen(BrightcovePlayerView view, boolean fullscreen) {
111112
@Override
112113
public Map<String, Integer> getCommandsMap() {
113114
return MapBuilder.of(
114-
"seekTo",
115-
COMMAND_SEEK_TO
115+
"seekTo", COMMAND_SEEK_TO,
116+
"setFullscreen", COMMAND_SET_FULLSCREEN
116117
);
117118
}
118119

@@ -125,6 +126,14 @@ public void receiveCommand(BrightcovePlayerView view, int commandType, @Nullable
125126
view.seekTo((int)(args.getDouble(0) * 1000));
126127
return;
127128
}
129+
case COMMAND_SET_FULLSCREEN: {
130+
if (args.getBoolean(0)) {
131+
view.dispatchEnterFullScreenClickEvent();
132+
} else {
133+
view.dispatchExitFullScreenClickEvent();
134+
}
135+
return;
136+
}
128137
}
129138
}
130139

android/src/main/java/jp/manse/BrightcovePlayerView.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,25 @@ public void processEvent(Event e) {
169169
});
170170
}
171171

172+
public void dispatchEnterFullScreenClickEvent() {
173+
this.playerVideoView.getEventEmitter().emit(EventType.ENTER_FULL_SCREEN);
174+
}
175+
176+
public void dispatchExitFullScreenClickEvent() {
177+
this.playerVideoView.getEventEmitter().emit(EventType.EXIT_FULL_SCREEN);
178+
}
179+
172180
@Override
173181
public void onConfigurationChanged(Configuration newConfig) {
174182
super.onConfigurationChanged(newConfig);
183+
adjustMediaControllerDimensions();
184+
}
185+
186+
private void adjustMediaControllerDimensions() {
175187
mediaController.show();
176188
mediaController.getBrightcoveControlBar().setVisibility(VISIBLE);
177189
mediaController.getBrightcoveControlBar().setMinimumWidth(getWidth());
190+
mediaController.getBrightcoveControlBar().setAlign(true);
178191
}
179192

180193
public void setPolicyKey(String policyKey) {

example/android/app/src/main/java/com/example/MainActivity.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.example;
22

3+
import android.content.Intent;
4+
import android.content.res.Configuration;
5+
36
import com.facebook.react.ReactActivity;
47
import com.facebook.react.ReactActivityDelegate;
58
import com.facebook.react.ReactRootView;
@@ -25,4 +28,12 @@ protected ReactRootView createRootView() {
2528
}
2629
};
2730
}
31+
32+
@Override
33+
public void onConfigurationChanged(Configuration newConfig) {
34+
super.onConfigurationChanged(newConfig);
35+
Intent intent = new Intent("onConfigurationChanged");
36+
intent.putExtra("newConfig", newConfig);
37+
this.sendBroadcast(intent);
38+
}
2839
}

example/components/BCPlayer.js

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,17 @@ import Orientation from 'react-native-orientation';
66

77
export default class BCPlayer extends Component {
88

9-
state = {
10-
orientation: null
9+
constructor(props) {
10+
super(props);
11+
12+
this.state = {
13+
orientation: null,
14+
forcedOrientation: false
15+
}
16+
17+
this.orientationDidChange = this.orientationDidChange.bind(this);
18+
this.onBeforeEnterFullscreen = this.onBeforeEnterFullscreen.bind(this);
19+
this.onBeforeExitFullscreen = this.onBeforeExitFullscreen.bind(this);
1120
}
1221

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

1928
const initial = Orientation.getInitialOrientation();
2029
this.setState({ orientation: initial });
30+
// Remember to remove listener
31+
Orientation.removeOrientationListener(this.orientationDidChange);
32+
}
33+
34+
componentDidMount() {
35+
Orientation.addOrientationListener(this.orientationDidChange);
2136
}
2237

2338
onBeforeEnterFullscreen() {
24-
Orientation.lockToLandscape();
39+
40+
if (this.state.orientation === 'PORTRAIT') {
41+
this.setState({ forcedOrientation: true });
42+
Orientation.lockToLandscape();
43+
}
44+
2545
this.props.onBeforeEnterFullscreen && this.props.onBeforeEnterFullscreen();
2646
}
2747

2848
onBeforeExitFullscreen() {
49+
this.setState({ forcedOrientation: false });
50+
2951
Orientation.lockToPortrait();
3052
Orientation.unlockAllOrientations();
53+
3154
this.props.onBeforeExitFullscreen && this.props.onBeforeExitFullscreen();
3255
}
3356

57+
orientationDidChange(orientation) {
58+
console.log("orientation did change");
59+
// If the player hasn't been loaded yet, then don't do anything
60+
if (!this.player) return;
61+
62+
switch (orientation) {
63+
case 'LANDSCAPE':
64+
// Only set the fullscreen in this case, if the forced orientation by the "lockTolandscape" hasn't been called
65+
// otherwise, if you call the setfullscreen twice, it might be buggy
66+
if (!this.state.forcedOrientation) {
67+
this.player.setFullscreen(true);
68+
}
69+
break;
70+
case 'PORTRAIT':
71+
this.player.setFullscreen(false);
72+
break;
73+
}
74+
75+
this.setState({ orientation });
76+
}
77+
3478
render() {
3579
return (<BrightcovePlayer
80+
ref={(player) => this.player = player}
3681
{...this.props}
3782
style={[styles.player, this.props.style]}
38-
onBeforeEnterFullscreen={this.onBeforeEnterFullscreen.bind(this)}
39-
onBeforeExitFullscreen={this.onBeforeExitFullscreen.bind(this)}
83+
onBeforeEnterFullscreen={this.onBeforeEnterFullscreen}
84+
onBeforeExitFullscreen={this.onBeforeExitFullscreen}
4085
/>
41-
4286
);
4387
}
4488
}

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export class BrightcovePlayer extends React.Component<
3535
{}
3636
> {
3737
seekTo(position: number): {};
38+
setFullscreen(fullscreen: boolean): {};
3839
}
3940

4041
export type BrightcovePlayerPosterProps = {

ios/BrightcovePlayer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
@property (nonatomic, copy) RCTDirectEventBlock onExitFullscreen;
4545

4646
-(void) seekTo:(NSNumber *)time;
47+
-(void) setFullscreen:(BOOL *)fullscreen;
4748
-(void)dispose;
4849

4950
@end

ios/BrightcovePlayerManager.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ - (dispatch_queue_t)methodQueue {
4949
}];
5050
}
5151

52+
RCT_EXPORT_METHOD(setFullscreen:(nonnull NSNumber *)reactTag fullscreen:(BOOL)fullscreen) {
53+
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
54+
BrightcovePlayer *player = (BrightcovePlayer*)viewRegistry[reactTag];
55+
if ([player isKindOfClass:[BrightcovePlayer class]]) {
56+
[player setFullscreen:fullscreen];
57+
}
58+
}];
59+
}
60+
5261
RCT_EXPORT_METHOD(dispose:(nonnull NSNumber *)reactTag) {
5362
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
5463
BrightcovePlayer *player = (BrightcovePlayer*)viewRegistry[reactTag];

src/BrightcovePlayer.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,28 @@ BrightcovePlayer.prototype.seekTo = Platform.select({
100100
android: function(seconds) {
101101
UIManager.dispatchViewManagerCommand(
102102
ReactNative.findNodeHandle(this._root),
103-
UIManager.BrightcovePlayer.Commands.seekTo,
103+
UIManager.getViewManagerConfig('BrightcovePlayer').Commands.seekTo,
104104
[seconds]
105105
);
106106
}
107107
});
108108

109+
BrightcovePlayer.prototype.setFullscreen = Platform.select({
110+
ios: function(fullscreen) {
111+
NativeModules.BrightcovePlayerManager.setFullscreen(
112+
ReactNative.findNodeHandle(this),
113+
fullscreen
114+
);
115+
},
116+
android: function(fullscreen) {
117+
UIManager.dispatchViewManagerCommand(
118+
ReactNative.findNodeHandle(this._root),
119+
UIManager.getViewManagerConfig('BrightcovePlayer').Commands.setFullscreen,
120+
[fullscreen]
121+
);
122+
}
123+
});
124+
109125
BrightcovePlayer.propTypes = {
110126
...(ViewPropTypes || View.propTypes),
111127
policyKey: PropTypes.string,

0 commit comments

Comments
 (0)