-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathPlayerScripts.js
246 lines (221 loc) · 7.1 KB
/
PlayerScripts.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import {MUTE_MODE, PAUSE_MODE, PLAY_MODE, UNMUTE_MODE} from './constants';
export const PLAYER_FUNCTIONS = {
muteVideo: 'player.mute(); true;',
unMuteVideo: 'player.unMute(); true;',
playVideo: 'player.playVideo(); true;',
pauseVideo: 'player.pauseVideo(); true;',
getVideoUrlScript: `
window.ReactNativeWebView.postMessage(JSON.stringify({eventType: 'getVideoUrl', data: player.getVideoUrl()}));
true;
`,
durationScript: `
window.ReactNativeWebView.postMessage(JSON.stringify({eventType: 'getDuration', data: player.getDuration()}));
true;
`,
currentTimeScript: `
window.ReactNativeWebView.postMessage(JSON.stringify({eventType: 'getCurrentTime', data: player.getCurrentTime()}));
true;
`,
isMutedScript: `
window.ReactNativeWebView.postMessage(JSON.stringify({eventType: 'isMuted', data: player.isMuted()}));
true;
`,
getVolumeScript: `
window.ReactNativeWebView.postMessage(JSON.stringify({eventType: 'getVolume', data: player.getVolume()}));
true;
`,
getPlaybackRateScript: `
window.ReactNativeWebView.postMessage(JSON.stringify({eventType: 'getPlaybackRate', data: player.getPlaybackRate()}));
true;
`,
getAvailablePlaybackRatesScript: `
window.ReactNativeWebView.postMessage(JSON.stringify({eventType: 'getAvailablePlaybackRates', data: player.getAvailablePlaybackRates()}));
true;
`,
setVolume: volume => {
return `player.setVolume(${volume}); true;`;
},
seekToScript: (seconds, allowSeekAhead) => {
return `player.seekTo(${seconds}, ${allowSeekAhead}); true;`;
},
setPlaybackRate: playbackRate => {
return `player.setPlaybackRate(${playbackRate}); true;`;
},
loadPlaylist: (playList, startIndex, play) => {
const index = startIndex || 0;
const playlistJson = JSON.stringify(playList);
const func = play ? 'loadPlaylist' : 'cuePlaylist';
return `player.${func}({playlist: ${playlistJson}, index: ${index}); true;`;
},
};
export const playMode = {
[PLAY_MODE]: PLAYER_FUNCTIONS.playVideo,
[PAUSE_MODE]: PLAYER_FUNCTIONS.pauseVideo,
};
export const soundMode = {
[MUTE_MODE]: PLAYER_FUNCTIONS.muteVideo,
[UNMUTE_MODE]: PLAYER_FUNCTIONS.unMuteVideo,
};
export const MAIN_SCRIPT = (
videoId,
playList,
initialPlayerParams,
allowWebViewZoom,
contentScale,
) => {
const {
end,
rel,
color,
start,
playerLang,
loop = false,
cc_lang_pref,
iv_load_policy,
modestbranding,
controls = true,
showClosedCaptions,
preventFullScreen = false,
} = initialPlayerParams;
// _s postfix to refer to "safe"
const rel_s = rel ? 1 : 0;
const loop_s = loop ? 1 : 0;
const videoId_s = videoId || '';
const controls_s = controls ? 1 : 0;
const cc_lang_pref_s = cc_lang_pref || '';
const modestbranding_s = modestbranding ? 1 : 0;
const preventFullScreen_s = preventFullScreen ? 0 : 1;
const showClosedCaptions_s = showClosedCaptions ? 1 : 0;
const list = typeof playList === 'string' ? playList : '';
const listType = typeof playList === 'string' ? 'playlist' : '';
const contentScale_s = typeof contentScale === 'number' ? contentScale : 1.0;
const playlist = Array.isArray(playList)
? `playlist: "${playList.join(',')}",`
: '';
// scale will either be "initial-scale=1.0"
let scale = `initial-scale=${contentScale_s}`;
if (!allowWebViewZoom) {
// or "initial-scale=0.8, maximum-scale=1.0"
scale += `, maximum-scale=${contentScale_s}`;
}
const safeData = {
end,
list,
start,
color,
rel_s,
loop_s,
listType,
playlist,
videoId_s,
controls_s,
playerLang,
iv_load_policy,
contentScale_s,
cc_lang_pref_s,
allowWebViewZoom,
modestbranding_s,
preventFullScreen_s,
showClosedCaptions_s,
};
const urlEncodedJSON = encodeURI(JSON.stringify(safeData));
const htmlString = `
<!DOCTYPE html>
<html>
<head>
<meta
name="viewport"
content="width=device-width, ${scale}"
>
<style>
body {
margin: 0;
}
.container {
position: relative;
width: 100%;
height: 0;
padding-bottom: 56.25%;
}
.video {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div class="container">
<div class="video" id="player" />
</div>
<script>
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
width: '1000',
height: '1000',
videoId: '${videoId_s}',
playerVars: {
${playlist}
end: ${end},
rel: ${rel_s},
playsinline: 1,
loop: ${loop_s},
color: ${color},
start: ${start},
list: '${list}',
hl: ${playerLang},
listType: '${listType}',
controls: ${controls_s},
fs: ${preventFullScreen_s},
cc_lang_pref: '${cc_lang_pref_s}',
iv_load_policy: ${iv_load_policy},
modestbranding: ${modestbranding_s},
cc_load_policy: ${showClosedCaptions_s},
},
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange,
'onError': onPlayerError,
'onPlaybackQualityChange': onPlaybackQualityChange,
'onPlaybackRateChange': onPlaybackRateChange,
}
});
}
function onPlayerError(event) {
window.ReactNativeWebView.postMessage(JSON.stringify({eventType: 'playerError', data: event.data}))
}
function onPlaybackRateChange(event) {
window.ReactNativeWebView.postMessage(JSON.stringify({eventType: 'playbackRateChange', data: event.data}))
}
function onPlaybackQualityChange(event) {
window.ReactNativeWebView.postMessage(JSON.stringify({eventType: 'playerQualityChange', data: event.data}))
}
function onPlayerReady(event) {
window.ReactNativeWebView.postMessage(JSON.stringify({eventType: 'playerReady'}))
}
var done = false;
function onPlayerStateChange(event) {
window.ReactNativeWebView.postMessage(JSON.stringify({eventType: 'playerStateChange', data: event.data}))
}
var isFullScreen = false;
function onFullScreenChange() {
isFullScreen = document.fullscreenElement || document.mozFullScreenElement || document.webkitFullscreenElement || document.msFullscreenElement;
window.ReactNativeWebView.postMessage(JSON.stringify({eventType: 'fullScreenChange', data: Boolean(isFullScreen)}));
}
document.addEventListener('fullscreenchange', onFullScreenChange)
document.addEventListener('mozfullscreenchange', onFullScreenChange)
document.addEventListener('msfullscreenchange', onFullScreenChange)
document.addEventListener('webkitfullscreenchange', onFullScreenChange)
</script>
</body>
</html>
`;
return {htmlString, urlEncodedJSON};
};