Added fullscreen options for iOS Player

This commit is contained in:
Ash Mishra 2018-08-09 09:58:03 -07:00
parent 0c03932ada
commit f41831ceac
5 changed files with 328 additions and 269 deletions

View File

@ -289,6 +289,11 @@ Video.propTypes = {
PropTypes.object PropTypes.object
]), ]),
fullscreen: PropTypes.bool, fullscreen: PropTypes.bool,
fullscreenOptions: PropTypes.shape({
enabled: PropTypes.bool,
preferredOrientation: PropTypes.string,
autorotate: PropTypes.bool
}),
onVideoLoadStart: PropTypes.func, onVideoLoadStart: PropTypes.func,
onVideoLoad: PropTypes.func, onVideoLoad: PropTypes.func,
onVideoBuffer: PropTypes.func, onVideoBuffer: PropTypes.func,

View File

@ -56,6 +56,7 @@ static int const RCTVideoUnset = -1;
NSString * _ignoreSilentSwitch; NSString * _ignoreSilentSwitch;
NSString * _resizeMode; NSString * _resizeMode;
BOOL _fullscreen; BOOL _fullscreen;
NSDictionary* _fullscreenOptions;
BOOL _fullscreenPlayerPresented; BOOL _fullscreenPlayerPresented;
UIViewController * _presentingViewController; UIViewController * _presentingViewController;
} }
@ -106,9 +107,16 @@ static int const RCTVideoUnset = -1;
} }
- (AVPlayerViewController*)createPlayerViewController:(AVPlayer*)player withPlayerItem:(AVPlayerItem*)playerItem { - (AVPlayerViewController*)createPlayerViewController:(AVPlayer*)player withPlayerItem:(AVPlayerItem*)playerItem {
RCTVideoPlayerViewController* playerLayer= [[RCTVideoPlayerViewController alloc] init]; RCTVideoPlayerViewController* playerLayer= [[RCTVideoPlayerViewController alloc] init];
playerLayer.showsPlaybackControls = YES; playerLayer.showsPlaybackControls = YES;
playerLayer.rctDelegate = self; playerLayer.rctDelegate = self;
if (_fullscreenOptions) {
playerLayer.preferredOrientation = [RCTConvert NSString:[_fullscreenOptions objectForKey:@"preferredOrientation"]];
playerLayer.autorotate = [RCTConvert BOOL:[_fullscreenOptions objectForKey:@"autorotate"]];
}
playerLayer.view.frame = self.bounds; playerLayer.view.frame = self.bounds;
playerLayer.player = player; playerLayer.player = player;
playerLayer.view.frame = self.bounds; playerLayer.view.frame = self.bounds;
@ -331,7 +339,7 @@ static int const RCTVideoUnset = -1;
[self addPlayerTimeObserver]; [self addPlayerTimeObserver];
[self setFullscreen:_fullscreen]; [self setFullscreenOptions:_fullscreenOptions];
//Perform on next run loop, otherwise onVideoLoadStart is nil //Perform on next run loop, otherwise onVideoLoadStart is nil
if(self.onVideoLoadStart) { if(self.onVideoLoadStart) {
@ -995,10 +1003,34 @@ static int const RCTVideoUnset = -1;
return _fullscreenPlayerPresented; return _fullscreenPlayerPresented;
} }
- (void)setFullscreen:(BOOL)fullscreen - (void)setFullscreen:(BOOL) fullscreen {
{
_fullscreen = fullscreen; _fullscreen = fullscreen;
if( fullscreen && !_fullscreenPlayerPresented && _player )
NSString* enabled = _fullscreen ? @"1" : @"0";
if (!_fullscreenOptions) {
[self setFullscreenOptions:
@{
@"enabled": enabled,
@"autorotate": @"0",
@"preferredOrientation": @"default"
}];
}
else {
[_fullscreenOptions setValue:enabled forKey:@"enabled"];
[self setFullscreenOptions:_fullscreenOptions];
}
}
- (void)setFullscreenOptions:(NSDictionary*) fullscreenOptions
{
_fullscreenOptions = fullscreenOptions;
if (!_fullscreenOptions) return;
BOOL fullscreenEnabled = [RCTConvert BOOL:[_fullscreenOptions objectForKey:@"enabled"]];
if( fullscreenEnabled && !_fullscreenPlayerPresented && _player )
{ {
// Ensure player view controller is not null // Ensure player view controller is not null
if( !_playerViewController ) if( !_playerViewController )
@ -1027,14 +1059,14 @@ static int const RCTVideoUnset = -1;
} }
[viewController presentViewController:_playerViewController animated:true completion:^{ [viewController presentViewController:_playerViewController animated:true completion:^{
_playerViewController.showsPlaybackControls = YES; _playerViewController.showsPlaybackControls = YES;
_fullscreenPlayerPresented = fullscreen; _fullscreenPlayerPresented = fullscreenEnabled;
if(self.onVideoFullscreenPlayerDidPresent) { if(self.onVideoFullscreenPlayerDidPresent) {
self.onVideoFullscreenPlayerDidPresent(@{@"target": self.reactTag}); self.onVideoFullscreenPlayerDidPresent(@{@"target": self.reactTag});
} }
}]; }];
} }
} }
else if ( !fullscreen && _fullscreenPlayerPresented ) else if ( !fullscreenEnabled && _fullscreenPlayerPresented )
{ {
[self videoPlayerViewControllerWillDismiss:_playerViewController]; [self videoPlayerViewControllerWillDismiss:_playerViewController];
[_presentingViewController dismissViewControllerAnimated:true completion:^{ [_presentingViewController dismissViewControllerAnimated:true completion:^{

View File

@ -36,7 +36,8 @@ RCT_EXPORT_VIEW_PROPERTY(ignoreSilentSwitch, NSString);
RCT_EXPORT_VIEW_PROPERTY(rate, float); RCT_EXPORT_VIEW_PROPERTY(rate, float);
RCT_EXPORT_VIEW_PROPERTY(seek, NSDictionary); RCT_EXPORT_VIEW_PROPERTY(seek, NSDictionary);
RCT_EXPORT_VIEW_PROPERTY(currentTime, float); RCT_EXPORT_VIEW_PROPERTY(currentTime, float);
RCT_EXPORT_VIEW_PROPERTY(fullscreen, BOOL); RCT_EXPORT_VIEW_PROPERTY(fullscreen, BOOL); // deprecated property
RCT_EXPORT_VIEW_PROPERTY(fullscreenOptions, NSDictionary);
RCT_EXPORT_VIEW_PROPERTY(progressUpdateInterval, float); RCT_EXPORT_VIEW_PROPERTY(progressUpdateInterval, float);
/* Should support: onLoadStart, onLoad, and onError to stay consistent with Image */ /* Should support: onLoadStart, onLoad, and onError to stay consistent with Image */
RCT_EXPORT_VIEW_PROPERTY(onVideoLoadStart, RCTBubblingEventBlock); RCT_EXPORT_VIEW_PROPERTY(onVideoLoadStart, RCTBubblingEventBlock);

View File

@ -12,4 +12,9 @@
@interface RCTVideoPlayerViewController : AVPlayerViewController @interface RCTVideoPlayerViewController : AVPlayerViewController
@property (nonatomic, weak) id<RCTVideoPlayerViewControllerDelegate> rctDelegate; @property (nonatomic, weak) id<RCTVideoPlayerViewControllerDelegate> rctDelegate;
// Optional paramters
@property (nonatomic, weak) NSString* preferredOrientation;
@property (nonatomic) BOOL autorotate;
@end @end

View File

@ -6,6 +6,14 @@
@implementation RCTVideoPlayerViewController @implementation RCTVideoPlayerViewController
- (id)init {
self = [super init];
if (self) {
self.autorotate = false;
}
return self;
}
- (void)viewDidDisappear:(BOOL)animated - (void)viewDidDisappear:(BOOL)animated
{ {
[super viewDidDisappear:animated]; [super viewDidDisappear:animated];
@ -14,17 +22,25 @@
} }
- (BOOL)shouldAutorotate { - (BOOL)shouldAutorotate {
return YES; return self.autorotate;
} }
- (UIInterfaceOrientationMask)supportedInterfaceOrientations { - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape; return UIInterfaceOrientationMaskAll;
} }
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
if ([self.preferredOrientation.lowercaseString isEqualToString:@"landscape"]) {
return UIInterfaceOrientationLandscapeLeft; return UIInterfaceOrientationLandscapeLeft;
} }
else if ([self.preferredOrientation.lowercaseString isEqualToString:@"portrait"]) {
return UIInterfaceOrientationPortrait;
}
else { // default case
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
return orientation;
}
}
@end @end