forked from johnteee/react-native-youtube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
YouTube.android.js
154 lines (137 loc) · 4.51 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
/**
* @providesModule YouTube
* @flow
*/
'use strict';
import React, { Component, PropTypes } from 'react';
import ReactNative, {
View,
StyleSheet,
requireNativeComponent,
NativeModules,
NativeMethodsMixin,
WebView,
Platform,
} from 'react-native';
const RCTYouTube = requireNativeComponent('ReactYouTube', YouTube,
{
nativeOnly: {onError:true,
onReady:true,
onChangeState:true,
onChangeQuality:true
}
});
export default class YouTube extends Component {
static propTypes = {
style: View.propTypes.style,
videoId: PropTypes.string.isRequired,
apiKey: PropTypes.string.isRequired,
playsInline: PropTypes.bool,
showinfo: PropTypes.bool,
modestbranding: PropTypes.bool,
controls: PropTypes.oneOf([0,1,2]),
origin: PropTypes.string,
play: PropTypes.bool,
rel: PropTypes.bool,
hidden: PropTypes.bool,
onReady: PropTypes.func,
onChangeState: PropTypes.func,
onChangeQuality: PropTypes.func,
onError: PropTypes.func,
loop: PropTypes.bool,
};
static defaultProps = {
loop: false
};
constructor(props) {
super(props);
this._exportedProps = NativeModules.YouTubeManager && NativeModules.YouTubeManager.exportedProps;
}
_onReady(event) {
return this.props.onReady && this.props.onReady(event.nativeEvent);
}
_onChangeState(event) {
if(event.nativeEvent.state == 'ended' && this.props.loop) {
this.seekTo(0);
}
return this.props.onChangeState && this.props.onChangeState(event.nativeEvent);
}
_onChangeQuality(event) {
return this.props.onChangeQuality && this.props.onChangeQuality(event.nativeEvent);
}
_onError(event) {
return this.props.onError && this.props.onError(event.nativeEvent);
}
_onProgress(event){
return this.props.onProgress && this.props.onProgress(event.nativeEvent);
}
seekTo(seconds){
NativeModules.YouTubeModule.seekTo(parseInt(seconds, 10));
//this.refs.youtubePlayer.seekTo(ReactNative.findNodeHandle(this), parseInt(seconds, 10));
}
render() {
var style = [styles.base, this.props.style];
var nativeProps = Object.assign({}, this.props);
nativeProps.style = style;
nativeProps.onYoutubeVideoReady = this._onReady.bind(this);
nativeProps.onYoutubeVideoChangeState = this._onChangeState.bind(this);
nativeProps.onYoutubeVideoChangeQuality = this._onChangeQuality.bind(this);
nativeProps.onYoutubeVideoError = this._onError.bind(this);
nativeProps.onYoutubeProgress = this._onProgress.bind(this);
/*
* Try to use `playerParams` instead of settings `playsInline` and
* `videoId` individually.
*/
if (this._exportedProps) {
if (this._exportedProps.playerParams) {
nativeProps.playerParams = {
videoId: this.props.videoId,
};
delete nativeProps.videoId;
nativeProps.playerParams.playerVars = {};
if (this.props.playsInline) {
nativeProps.playerParams.playerVars.playsinline = 1;
delete nativeProps.playsInline;
};
if (this.props.modestbranding) {
nativeProps.playerParams.playerVars.modestbranding = 1;
delete nativeProps.modestbranding;
};
if (this.props.showinfo!==undefined) {
nativeProps.playerParams.playerVars.showinfo = this.props.showinfo ? 1 : 0;
delete nativeProps.showinfo;
};
if (this.props.controls!==undefined) {
nativeProps.playerParams.playerVars.controls = this.props.controls;
delete nativeProps.controls;
};
if (this.props.origin!==undefined) {
nativeProps.playerParams.playerVars.origin = this.props.origin;
delete nativeProps.origin;
};
if (this.props.rel!==undefined) {
nativeProps.playerParams.playerVars.rel = this.props.rel ? 1 : 0;
delete nativeProps.rel;
};
};
} else {
/*
* For compatibility issues with an older version where setting both
* `playsInline` and `videoId` in quick succession would cause the video
* to sometimes not play.
*/
delete nativeProps.playsInline;
}
if(Platform.OS === 'ios'){
return <RCTYouTube {... nativeProps} />
}
else{
return <WebView style={this.props.style} source={{html: `<iframe width='320' height='300' src="https://www.youtube.com/embed/${this.props.videoId}" frameborder="0" allowfullscreen></iframe>`}} />
}
}
}
const styles = StyleSheet.create({
base: {
overflow: 'hidden',
}
});