From e504944ef593d947f34d3a1acbb75c07ab03e7c4 Mon Sep 17 00:00:00 2001 From: Brent Vatne Date: Sat, 4 Apr 2015 17:55:37 -0700 Subject: [PATCH] Swap out MPMoviePlayerController for AVPlayer - Better performance, can play multiple videos on one screen too! --- RCTVideo.m | 60 +++++++++++++++++++++++++++++++---------------- RCTVideoManager.m | 14 +++++------ 2 files changed, 47 insertions(+), 27 deletions(-) diff --git a/RCTVideo.m b/RCTVideo.m index f211f4e3..cc5101c4 100644 --- a/RCTVideo.m +++ b/RCTVideo.m @@ -1,18 +1,17 @@ #import "RCTVideo.h" #import "RCTLog.h" - -@import MediaPlayer; +#import @implementation RCTVideo { - MPMoviePlayerController *_player; + AVPlayer *_player; + AVPlayerLayer *_playerLayer; } - (id)init { if ((self = [super init])) { - _player = [[MPMoviePlayerController alloc] init]; - [self addSubview: _player.view]; + } return self; } @@ -20,33 +19,49 @@ - (void)setSrc:(NSString *)source { NSURL *videoURL = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:source ofType:@"mp4"]]; - [_player setContentURL:videoURL]; - [_player setControlStyle:MPMovieControlStyleNone]; - [_player setScalingMode:MPMovieScalingModeNone]; - [_player prepareToPlay]; + _player = [AVPlayer playerWithURL:videoURL]; + _player.actionAtItemEnd = AVPlayerActionAtItemEndNone; + _playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player]; + _playerLayer.frame = self.bounds; + _playerLayer.needsDisplayOnBoundsChange = YES; + [self.layer addSublayer:_playerLayer]; + self.layer.needsDisplayOnBoundsChange = YES; [_player play]; } -- (void)setResizeMode:(NSInteger)mode +- (void)setResizeMode:(NSString*)mode { - [_player setScalingMode:mode]; + _playerLayer.videoGravity = mode; +} + + +- (void)playerItemDidReachEnd:(NSNotification *)notification { + AVPlayerItem *item = [notification object]; + [item seekToTime:kCMTimeZero]; + [_player play]; +} + + +- (void)setRepeatEnabled { + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(playerItemDidReachEnd:) + name:AVPlayerItemDidPlayToEndTimeNotification + object:[_player currentItem]]; +} + +- (void) setRepeatDisabled { + [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)setRepeat:(BOOL)repeat { if (repeat) { - [_player setRepeatMode:MPMovieRepeatModeOne]; + [self setRepeatEnabled]; } else { - [_player setRepeatMode:MPMovieRepeatModeNone]; + [self setRepeatDisabled]; } } -- (NSArray *)reactSubviews -{ - NSArray *subviews = @[_player.view]; - return subviews; -} - - (void)insertReactSubview:(UIView *)view atIndex:(NSInteger)atIndex { RCTLogError(@"video cannot have any subviews"); @@ -62,7 +77,12 @@ - (void)layoutSubviews { [super layoutSubviews]; - _player.view.frame = self.bounds; + _playerLayer.frame = self.bounds; +} + +- (void)dealloc +{ + [[NSNotificationCenter defaultCenter] removeObserver:self]; } @end diff --git a/RCTVideoManager.m b/RCTVideoManager.m index 08acd0fc..49e08239 100644 --- a/RCTVideoManager.m +++ b/RCTVideoManager.m @@ -1,7 +1,7 @@ #import "RCTVideoManager.h" #import "RCTVideo.h" #import "RCTBridge.h" -@import MediaPlayer; +#import @implementation RCTVideoManager @@ -13,15 +13,15 @@ } RCT_EXPORT_VIEW_PROPERTY(src, NSString); -RCT_EXPORT_VIEW_PROPERTY(resizeMode, NSInteger); +RCT_EXPORT_VIEW_PROPERTY(resizeMode, NSString); RCT_EXPORT_VIEW_PROPERTY(repeat, BOOL); - (NSDictionary *)constantsToExport { - return @{@"ScaleNone": @(MPMovieScalingModeNone), - @"ScaleToFill": @(MPMovieScalingModeFill), - @"ScaleAspectFit": @(MPMovieScalingModeAspectFit), - @"ScaleAspectFill": @(MPMovieScalingModeAspectFill)}; + return @{@"ScaleNone": AVLayerVideoGravityResizeAspect, + @"ScaleToFill": AVLayerVideoGravityResize, + @"ScaleAspectFit": AVLayerVideoGravityResizeAspect, + @"ScaleAspectFill": AVLayerVideoGravityResizeAspectFill}; } -@end +@end \ No newline at end of file