forked from davidohayon669/react-native-youtube
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RCTYouTube.m
244 lines (206 loc) · 6.68 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
//
// MYNReactYouTubeView.m
// Myntra
//
// Created by Param Aggarwal on 15/06/15.
// Copyright (c) 2015 Myntra Designs. All rights reserved.
//
#import "RCTYouTube.h"
#import "RCTBridgeModule.h"
#import "RCTEventDispatcher.h"
#import "UIView+React.h"
@implementation RCTYouTube
{
NSString *_videoId;
BOOL _playsInline;
NSDictionary *_playerParams;
BOOL _isPlaying;
/* Check to see if commands can
* be sent to the player
*/
BOOL _isReady;
BOOL _playsOnLoad;
/* Required to publish events */
RCTEventDispatcher *_eventDispatcher;
}
- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher
{
if ((self = [super init])) {
_eventDispatcher = eventDispatcher;
_playsInline = NO;
_isPlaying = NO;
self.delegate = self;
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
if (self.webView) {
self.webView.frame = self.bounds;
}
}
#pragma mark - YTPlayer control methods
- (void)setPlay:(BOOL)play {
// if not ready, configure for later
if (!_isReady) {
_playsOnLoad = play;
return;
}
if (!_isPlaying && play) {
[self playVideo];
_isPlaying = YES;
} else if (_isPlaying && !play) {
[self pauseVideo];
_isPlaying = NO;
}
}
- (void)setPlaysInline:(BOOL)playsInline {
if (_videoId && playsInline) {
[self loadWithVideoId:_videoId playerVars:@{@"playsinline": @1}];
} else if (_videoId && !playsInline){
[self loadWithVideoId:_videoId];
} else {
// will get set when videoId is set
}
_playsInline = playsInline;
}
- (void)setVideoId:(NSString *)videoId {
if (_videoId) {
[self cueVideoById:videoId startSeconds:0 suggestedQuality:kYTPlaybackQualityDefault];
} else if (_playsInline) {
[self loadWithVideoId:videoId playerVars:@{@"playsinline": @1}];
} else {
// will get set when playsInline is set
}
_videoId = videoId;
}
- (void)setPlayerParams:(NSDictionary *)playerParams {
_playerParams = playerParams;
[self loadWithPlayerParams:playerParams];
}
#pragma mark - YTPlayer delegate methods
- (void)playerViewDidBecomeReady:(YTPlayerView *)playerView {
if (_playsOnLoad) {
[self playVideo];
_isPlaying = YES;
}
_isReady = YES;
[_eventDispatcher sendInputEventWithName:@"youtubeVideoReady"
body:@{
@"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;
}
[_eventDispatcher sendInputEventWithName:@"youtubeVideoChangeState"
body:@{
@"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;
}
[_eventDispatcher sendInputEventWithName:@"youtubeVideoChangeQuality"
body:@{
@"quality": playerQuality,
@"target": self.reactTag
}];
}
- (void)playerView:(YTPlayerView *)playerView didPlayTime:(float)currentTime {
[_eventDispatcher sendInputEventWithName:@"youtubeProgress"
body:@{
@"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;
}
[_eventDispatcher sendInputEventWithName:@"youtubeVideoError"
body:@{
@"error": playerError,
@"target": self.reactTag
}];
}
#pragma mark - Lifecycle
- (void)removeFromSuperview
{
[self removeWebView];
[super removeFromSuperview];
}
@end