forked from davidohayon669/react-native-youtube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
YouTube.android.js
195 lines (171 loc) · 5.49 KB
/
YouTube.android.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* @providesModule YouTube
*/
import React from 'react';
import PropTypes from 'prop-types';
import ReactNative, {
View,
ViewPropTypes,
Text,
StyleSheet,
requireNativeComponent,
UIManager,
NativeModules,
BackAndroid,
BackHandler as BackHandlerModule,
} from 'react-native';
const BackHandler = BackHandlerModule || BackAndroid;
const RCTYouTube = requireNativeComponent('ReactYouTube', YouTube, {
nativeOnly: {
onYouTubeError: true,
onYouTubeErrorReady: true,
onYouTubeErrorChangeState: true,
onYouTubeErrorChangeQuality: true,
onYouTubeChangeFullscreen: true,
},
});
export default class YouTube extends React.Component {
static propTypes = {
apiKey: PropTypes.string.isRequired,
videoId: PropTypes.string,
videoIds: PropTypes.arrayOf(PropTypes.string),
playlistId: PropTypes.string,
play: PropTypes.bool,
loop: PropTypes.bool,
fullscreen: PropTypes.bool,
controls: PropTypes.oneOf([0, 1, 2]),
showFullscreenButton: PropTypes.bool,
onError: PropTypes.func,
onReady: PropTypes.func,
onChangeState: PropTypes.func,
onChangeQuality: PropTypes.func,
onChangeFullscreen: PropTypes.func,
style: (ViewPropTypes && ViewPropTypes.style) || View.propTypes.style,
};
static defaultProps = {
showFullscreenButton: true,
};
constructor(props) {
super(props);
if (props.playsInline !== undefined) {
throw new Error('YouTube.android.js: `playsInline` prop was dropped. Please use `fullscreen`');
}
this.state = {
moduleMargin: StyleSheet.hairlineWidth * 2,
fullscreen: props.fullscreen,
};
}
componentWillMount() {
BackHandler.addEventListener('hardwareBackPress', this._backPress);
}
componentWillReceiveProps(nextProps) {
// Translate next `fullscreen` prop to state
if (nextProps.fullscreen !== this.props.fullscreen) {
this.setState({ fullscreen: nextProps.fullscreen });
}
}
componentWillUnmount() {
BackHandler.removeEventListener('hardwareBackPress', this._backPress);
}
_backPress = () => {
if (this.state.fullscreen) {
this.setState({ fullscreen: false });
return true;
}
return false;
}
_onError = (event) => {
if (this.props.onError) this.props.onError(event.nativeEvent);
}
_onReady = (event) => {
// The Android YouTube native module is pretty problematic when it comes to
// mounting correctly and rendering inside React-Native's views hierarchy.
// For now we must trigger some layout change to force a real render on it,
// right after the onReady event, so it will smoothly appear after ready.
// We also use the minimal margin to avoid `UNAUTHORIZED_OVERLAY` error from
// the native module that is very sensitive to being covered or even touching
// its containing view.
this.setState({ moduleMargin: StyleSheet.hairlineWidth });
if (this.props.onReady) this.props.onReady(event.nativeEvent);
}
_onChangeState = (event) => {
if (this.props.onChangeState) this.props.onChangeState(event.nativeEvent);
}
_onChangeQuality = (event) => {
if (this.props.onChangeQuality) this.props.onChangeQuality(event.nativeEvent);
}
_onChangeFullscreen = (event) => {
const { isFullscreen } = event.nativeEvent;
if (this.state.fullscreen !== isFullscreen) this.setState({ fullscreen: isFullscreen });
if (this.props.onChangeFullscreen) this.props.onChangeFullscreen(event.nativeEvent);
}
seekTo(seconds) {
UIManager.dispatchViewManagerCommand(
ReactNative.findNodeHandle(this._nativeComponentRef),
UIManager.ReactYouTube.Commands.seekTo,
[parseInt(seconds, 10)],
);
}
nextVideo() {
UIManager.dispatchViewManagerCommand(
ReactNative.findNodeHandle(this._nativeComponentRef),
UIManager.ReactYouTube.Commands.nextVideo,
[],
);
}
previousVideo() {
UIManager.dispatchViewManagerCommand(
ReactNative.findNodeHandle(this._nativeComponentRef),
UIManager.ReactYouTube.Commands.previousVideo,
[],
);
}
playVideoAt(index) {
UIManager.dispatchViewManagerCommand(
ReactNative.findNodeHandle(this._nativeComponentRef),
UIManager.ReactYouTube.Commands.playVideoAt,
[parseInt(index, 10)],
);
}
videosIndex() {
return new Promise((resolve, reject) =>
NativeModules.YouTubeModule
.videosIndex(ReactNative.findNodeHandle(this._nativeComponentRef))
.then(index => resolve(index))
.catch(errorMessage => reject(errorMessage)));
}
currentTime() {
return new Promise((resolve, reject) =>
NativeModules.YouTubeModule
.currentTime(ReactNative.findNodeHandle(this._nativeComponentRef))
.then(currentTime => resolve(currentTime))
.catch(errorMessage => reject(errorMessage)));
}
render() {
return (
<View style={[styles.container, this.props.style]}>
<RCTYouTube
ref={(component) => {
this._nativeComponentRef = component;
}}
{...this.props}
fullscreen={this.state.fullscreen}
style={[styles.module, { margin: this.state.moduleMargin }]}
onYouTubeError={this._onError}
onYouTubeReady={this._onReady}
onYouTubeChangeState={this._onChangeState}
onYouTubeChangeQuality={this._onChangeQuality}
onYouTubeChangeFullscreen={this._onChangeFullscreen}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
backgroundColor: 'black',
},
module: {
flex: 1,
},
});