forked from davidohayon669/react-native-youtube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RCTYouTube.m
311 lines (264 loc) · 8.53 KB
/
RCTYouTube.m
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#import "RCTYouTube.h"
#if __has_include(<React/RCTAssert.h>)
#import <React/RCTBridge.h>
#import <React/UIView+React.h>
#else // backwards compatibility for RN < 0.40
#import "RCTBridge.h"
#import "UIView+React.h"
#endif
@interface RCTYouTube ()
@property (nonatomic, copy) RCTDirectEventBlock onError;
@property (nonatomic, copy) RCTDirectEventBlock onReady;
@property (nonatomic, copy) RCTDirectEventBlock onChangeState;
@property (nonatomic, copy) RCTDirectEventBlock onChangeQuality;
@property (nonatomic, copy) RCTDirectEventBlock onChangeFullscreen;
@property (nonatomic, copy) RCTDirectEventBlock onProgress;
@end
@implementation RCTYouTube {
__weak RCTBridge *_bridge;
BOOL _isReady;
BOOL _playOnLoad;
BOOL _loop;
/* StatusBar visibility status before the player changed to fullscreen */
// BOOL _isStatusBarHidden;
BOOL _isFullscreen;
}
- (instancetype)initWithBridge:(RCTBridge *)bridge {
if ((self = [super initWithFrame:CGRectZero])) {
_bridge = bridge;
_isReady = NO;
_playOnLoad = NO;
_loop = NO;
_isFullscreen = NO;
[self addFullscreenObserver];
self.delegate = self;
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame { @throw nil; }
- (instancetype)initWithCoder:(NSCoder *)aDecoder { @throw nil; }
- (void)addFullscreenObserver {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerFullscreenStateChange:)
name:UIWindowDidResignKeyNotification
object:self.window];
}
- (void)removeFullScreenObserver {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIWindowDidResignKeyNotification object:self.window];
}
- (void)dealloc {
[self removeFullScreenObserver];
}
- (void)layoutSubviews {
[super layoutSubviews];
if (self.webView) {
self.webView.frame = self.bounds;
if (@available(iOS 11.0, *)) {
self.webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
}
}
- (void)playerFullscreenStateChange:(NSNotification*)notification
{
if ((UIWindow*)notification.object == self.window && !_isFullscreen) {
_isFullscreen = YES;
_onChangeFullscreen(@{
@"isFullscreen": @(_isFullscreen),
@"target": self.reactTag
});
}
if ((UIWindow*)notification.object != self.window && _isFullscreen) {
_isFullscreen = NO;
_onChangeFullscreen(@{
@"isFullscreen": @(_isFullscreen),
@"target": self.reactTag
});
}
}
#pragma mark - YTPlayer control methods
- (void)setPlayerParams:(NSDictionary *)playerParams {
if (playerParams[@"videoId"]) {
[self loadWithVideoId:playerParams[@"videoId"]
playerVars:playerParams[@"playerVars"]];
} else if (playerParams[@"playlistId"]) {
[self loadWithPlaylistId:playerParams[@"playlistId"]
playerVars:playerParams[@"playerVars"]];
} else {
// if no videos info provided, we would still want to initiate an iframe instance
// so it'll be available for future method calls with the initial vars
[self loadWithVideoId:@""
playerVars:playerParams[@"playerVars"]];
}
}
- (void)setPlay:(BOOL)play {
if (!_isReady) {
_playOnLoad = play;
} else {
if (play) [self playVideo];
else [self pauseVideo];
}
}
- (void)setVideoId:(NSString *)videoId {
if (videoId && _isReady) {
if (_loop) {
// Looping a single video is unsupported by the iframe player so we
// must load the video as a 2 videos playlist, as suggested here:
// https://developers.google.com/youtube/player_parameters#loop
[self loadPlaylistByVideos:@[videoId, videoId]
index:0
startSeconds:0
suggestedQuality:kYTPlaybackQualityDefault];
} else {
[self loadVideoById:videoId
startSeconds:0
suggestedQuality:kYTPlaybackQualityDefault];
}
}
}
- (void)setVideoIds:(NSArray *)videoIds {
if (_isReady) {
[self loadPlaylistByVideos:videoIds
index:0
startSeconds:0
suggestedQuality:kYTPlaybackQualityDefault];
[self setLoopProp:_loop];
}
}
- (void)setPlaylistId:(NSString *)playlistId {
if (playlistId && _isReady) {
// TODO: there is an unidentifiable error with this method when using a playlist's id
[self loadPlaylistByPlaylistId:playlistId
index:0
startSeconds:0
suggestedQuality:kYTPlaybackQualityDefault];
}
}
- (void)setLoopProp:(BOOL)loop {
_loop = loop;
if (_isReady) [self setLoop:loop];
}
#pragma mark - YTPlayer delegate methods
- (void)playerViewDidBecomeReady:(YTPlayerView *)playerView {
if (_playOnLoad) [self playVideo];
_isReady = YES;
if (_onReady) {
_onReady(@{@"target": self.reactTag});
}
}
- (void)playerView:(YTPlayerView *)playerView didChangeToState:(YTPlayerState)state {
NSString *playerState;
switch (state) {
case kYTPlayerStateUnknown:
playerState = @"unknown";
break;
case kYTPlayerStateUnstarted:
playerState = @"unstarted";
break;
case kYTPlayerStateQueued:
playerState = @"queued";
break;
case kYTPlayerStateBuffering:
playerState = @"buffering";
break;
case kYTPlayerStatePlaying:
playerState = @"playing";
break;
case kYTPlayerStatePaused:
playerState = @"paused";
break;
case kYTPlayerStateEnded:
playerState = @"ended";
break;
default:
break;
}
if (_onChangeState) {
_onChangeState(@{
@"state": playerState,
@"target": self.reactTag
});
}
}
- (void)playerView:(YTPlayerView *)playerView didChangeToQuality:(YTPlaybackQuality)quality {
NSString *playerQuality;
switch (quality) {
case kYTPlaybackQualitySmall:
playerQuality = @"small";
break;
case kYTPlaybackQualityMedium:
playerQuality = @"medium";
break;
case kYTPlaybackQualityLarge:
playerQuality = @"large";
break;
case kYTPlaybackQualityHD720:
playerQuality = @"hd720";
break;
case kYTPlaybackQualityHD1080:
playerQuality = @"hd1080";
break;
case kYTPlaybackQualityHighRes:
playerQuality = @"high_res";
break;
case kYTPlaybackQualityAuto: /** Addition for YouTube Live Events. */
playerQuality = @"auto";
break;
case kYTPlaybackQualityDefault:
playerQuality = @"default";
break;
case kYTPlaybackQualityUnknown:
playerQuality = @"unknown";
break;
default:
break;
}
if (_onChangeQuality) {
_onChangeQuality(@{
@"quality": playerQuality,
@"target": self.reactTag
});
}
}
- (void)playerView:(YTPlayerView *)playerView didPlayTime:(float)currentTime {
if (_onProgress) {
_onProgress(@{
@"currentTime": @(currentTime),
@"duration": @(self.duration),
@"target": self.reactTag
});
}
}
- (void)playerView:(YTPlayerView *)playerView receivedError:(YTPlayerError)error {
NSString *playerError;
switch (error) {
case kYTPlayerErrorInvalidParam:
playerError = @"invalid_param";
break;
case kYTPlayerErrorHTML5Error:
playerError = @"html5_error";
break;
case kYTPlayerErrorVideoNotFound:
playerError = @"video_not_found";
break;
case kYTPlayerErrorNotEmbeddable:
playerError = @"not_embeddable";
break;
case kYTPlayerErrorUnknown:
playerError = @"unknown";
break;
default:
break;
}
if (_onError) {
_onError(@{
@"error": playerError,
@"target": self.reactTag
});
}
}
#pragma mark - Lifecycle
- (void)removeFromSuperview {
[self removeWebView];
[super removeFromSuperview];
}
@end