-
Notifications
You must be signed in to change notification settings - Fork 472
/
RCTYouTubeStandalone.m
54 lines (46 loc) · 2.19 KB
/
RCTYouTubeStandalone.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
#import "RCTYouTubeStandalone.h"
#if __has_include(<XCDYouTubeKit/XCDYouTubeKit.h>)
#import <XCDYouTubeKit/XCDYouTubeKit.h>
#define XCD_YOUTUBE_KIT_INSTALLED
#endif
@import AVKit;
@implementation RCTYouTubeStandalone {
RCTPromiseResolveBlock resolver;
RCTPromiseRejectBlock rejecter;
};
RCT_EXPORT_MODULE();
RCT_REMAP_METHOD(playVideo,
playVideoWithResolver:(NSString*)videoId
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
#ifndef XCD_YOUTUBE_KIT_INSTALLED
reject(@"error", @"XCDYouTubeKit is not installed. Refer to README for instructions.", nil);
#else
dispatch_async(dispatch_get_main_queue(), ^{
UIViewController *root = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
[root presentViewController:playerViewController animated:YES completion:nil];
__weak AVPlayerViewController *weakPlayerViewController = playerViewController;
[[XCDYouTubeClient defaultClient] getVideoWithIdentifier:videoId
completionHandler:^(XCDYouTubeVideo * _Nullable video, NSError * _Nullable error) {
if (video) {
NSDictionary *streamURLs = video.streamURLs;
NSURL *streamURL = streamURLs[
XCDYouTubeVideoQualityHTTPLiveStreaming] ?:
streamURLs[@(XCDYouTubeVideoQualityHD720)] ?:
streamURLs[@(XCDYouTubeVideoQualityMedium360)] ?:
streamURLs[@(XCDYouTubeVideoQualitySmall240)
];
weakPlayerViewController.player = [AVPlayer playerWithURL:streamURL];
[weakPlayerViewController.player play];
resolve(@"YouTubeStandaloneIOS player launched successfully");
} else {
reject(@"error", error.localizedDescription, nil);
[root dismissViewControllerAnimated:YES completion:nil];
}
}];
});
#endif
}
@end