@@ -6,8 +6,17 @@ import Orientation from 'react-native-orientation';
6
6
7
7
export default class BCPlayer extends Component {
8
8
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 ) ;
11
20
}
12
21
13
22
componentWillMount ( ) {
@@ -18,27 +27,62 @@ export default class BCPlayer extends Component {
18
27
19
28
const initial = Orientation . getInitialOrientation ( ) ;
20
29
this . setState ( { orientation : initial } ) ;
30
+ // Remember to remove listener
31
+ Orientation . removeOrientationListener ( this . orientationDidChange ) ;
32
+ }
33
+
34
+ componentDidMount ( ) {
35
+ Orientation . addOrientationListener ( this . orientationDidChange ) ;
21
36
}
22
37
23
38
onBeforeEnterFullscreen ( ) {
24
- Orientation . lockToLandscape ( ) ;
39
+
40
+ if ( this . state . orientation === 'PORTRAIT' ) {
41
+ this . setState ( { forcedOrientation : true } ) ;
42
+ Orientation . lockToLandscape ( ) ;
43
+ }
44
+
25
45
this . props . onBeforeEnterFullscreen && this . props . onBeforeEnterFullscreen ( ) ;
26
46
}
27
47
28
48
onBeforeExitFullscreen ( ) {
49
+ this . setState ( { forcedOrientation : false } ) ;
50
+
29
51
Orientation . lockToPortrait ( ) ;
30
52
Orientation . unlockAllOrientations ( ) ;
53
+
31
54
this . props . onBeforeExitFullscreen && this . props . onBeforeExitFullscreen ( ) ;
32
55
}
33
56
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
+
34
78
render ( ) {
35
79
return ( < BrightcovePlayer
80
+ ref = { ( player ) => this . player = player }
36
81
{ ...this . props }
37
82
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 }
40
85
/>
41
-
42
86
) ;
43
87
}
44
88
}
0 commit comments