forked from davidohayon669/react-native-youtube
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RCTYouTubeStandalone.m
62 lines (52 loc) · 2.34 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
55
56
57
58
59
60
61
62
#import "RCTYouTubeStandalone.h"
#if __has_include(<XCDYouTubeKit/XCDYouTubeKit.h>)
#import <XCDYouTubeKit/XCDYouTubeKit.h>
#define XCD_YOUTUBE_KIT_INSTALLED
#endif
@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", nil);
#else
dispatch_async(dispatch_get_main_queue(), ^{
XCDYouTubeVideoPlayerViewController *videoPlayerViewController =
[[XCDYouTubeVideoPlayerViewController alloc] initWithVideoIdentifier:videoId];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayerPlaybackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:videoPlayerViewController.moviePlayer];
resolver = resolve;
rejecter = reject;
UIViewController *root = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
[root presentMoviePlayerViewControllerAnimated:videoPlayerViewController];
});
#endif
}
#ifdef XCD_YOUTUBE_KIT_INSTALLED
- (void) moviePlayerPlaybackDidFinish:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:notification.object];
MPMovieFinishReason finishReason = [notification.userInfo[MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
if (finishReason == MPMovieFinishReasonPlaybackError)
{
NSError *error = notification.userInfo[XCDMoviePlayerPlaybackDidFinishErrorUserInfoKey];
// Handle error
rejecter(@"error", @"YTError", error);
} else {
resolver(@"success");
}
rejecter = nil;
resolver = nil;
}
#endif
@end