Swap out MPMoviePlayerController for AVPlayer
- Better performance, can play multiple videos on one screen too!
This commit is contained in:
parent
5ea65392a0
commit
e504944ef5
60
RCTVideo.m
60
RCTVideo.m
@ -1,18 +1,17 @@
|
|||||||
#import "RCTVideo.h"
|
#import "RCTVideo.h"
|
||||||
#import "RCTLog.h"
|
#import "RCTLog.h"
|
||||||
|
#import <AVFoundation/AVFoundation.h>
|
||||||
@import MediaPlayer;
|
|
||||||
|
|
||||||
@implementation RCTVideo
|
@implementation RCTVideo
|
||||||
{
|
{
|
||||||
MPMoviePlayerController *_player;
|
AVPlayer *_player;
|
||||||
|
AVPlayerLayer *_playerLayer;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (id)init
|
- (id)init
|
||||||
{
|
{
|
||||||
if ((self = [super init])) {
|
if ((self = [super init])) {
|
||||||
_player = [[MPMoviePlayerController alloc] init];
|
|
||||||
[self addSubview: _player.view];
|
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
@ -20,33 +19,49 @@
|
|||||||
- (void)setSrc:(NSString *)source
|
- (void)setSrc:(NSString *)source
|
||||||
{
|
{
|
||||||
NSURL *videoURL = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:source ofType:@"mp4"]];
|
NSURL *videoURL = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:source ofType:@"mp4"]];
|
||||||
[_player setContentURL:videoURL];
|
_player = [AVPlayer playerWithURL:videoURL];
|
||||||
[_player setControlStyle:MPMovieControlStyleNone];
|
_player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
|
||||||
[_player setScalingMode:MPMovieScalingModeNone];
|
_playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
|
||||||
[_player prepareToPlay];
|
_playerLayer.frame = self.bounds;
|
||||||
|
_playerLayer.needsDisplayOnBoundsChange = YES;
|
||||||
|
[self.layer addSublayer:_playerLayer];
|
||||||
|
self.layer.needsDisplayOnBoundsChange = YES;
|
||||||
[_player play];
|
[_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
|
- (void)setRepeat:(BOOL)repeat
|
||||||
{
|
{
|
||||||
if (repeat) {
|
if (repeat) {
|
||||||
[_player setRepeatMode:MPMovieRepeatModeOne];
|
[self setRepeatEnabled];
|
||||||
} else {
|
} else {
|
||||||
[_player setRepeatMode:MPMovieRepeatModeNone];
|
[self setRepeatDisabled];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSArray *)reactSubviews
|
|
||||||
{
|
|
||||||
NSArray *subviews = @[_player.view];
|
|
||||||
return subviews;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)insertReactSubview:(UIView *)view atIndex:(NSInteger)atIndex
|
- (void)insertReactSubview:(UIView *)view atIndex:(NSInteger)atIndex
|
||||||
{
|
{
|
||||||
RCTLogError(@"video cannot have any subviews");
|
RCTLogError(@"video cannot have any subviews");
|
||||||
@ -62,7 +77,12 @@
|
|||||||
- (void)layoutSubviews
|
- (void)layoutSubviews
|
||||||
{
|
{
|
||||||
[super layoutSubviews];
|
[super layoutSubviews];
|
||||||
_player.view.frame = self.bounds;
|
_playerLayer.frame = self.bounds;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)dealloc
|
||||||
|
{
|
||||||
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#import "RCTVideoManager.h"
|
#import "RCTVideoManager.h"
|
||||||
#import "RCTVideo.h"
|
#import "RCTVideo.h"
|
||||||
#import "RCTBridge.h"
|
#import "RCTBridge.h"
|
||||||
@import MediaPlayer;
|
#import <AVFoundation/AVFoundation.h>
|
||||||
|
|
||||||
@implementation RCTVideoManager
|
@implementation RCTVideoManager
|
||||||
|
|
||||||
@ -13,15 +13,15 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
RCT_EXPORT_VIEW_PROPERTY(src, NSString);
|
RCT_EXPORT_VIEW_PROPERTY(src, NSString);
|
||||||
RCT_EXPORT_VIEW_PROPERTY(resizeMode, NSInteger);
|
RCT_EXPORT_VIEW_PROPERTY(resizeMode, NSString);
|
||||||
RCT_EXPORT_VIEW_PROPERTY(repeat, BOOL);
|
RCT_EXPORT_VIEW_PROPERTY(repeat, BOOL);
|
||||||
|
|
||||||
- (NSDictionary *)constantsToExport
|
- (NSDictionary *)constantsToExport
|
||||||
{
|
{
|
||||||
return @{@"ScaleNone": @(MPMovieScalingModeNone),
|
return @{@"ScaleNone": AVLayerVideoGravityResizeAspect,
|
||||||
@"ScaleToFill": @(MPMovieScalingModeFill),
|
@"ScaleToFill": AVLayerVideoGravityResize,
|
||||||
@"ScaleAspectFit": @(MPMovieScalingModeAspectFit),
|
@"ScaleAspectFit": AVLayerVideoGravityResizeAspect,
|
||||||
@"ScaleAspectFill": @(MPMovieScalingModeAspectFill)};
|
@"ScaleAspectFill": AVLayerVideoGravityResizeAspectFill};
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
Loading…
Reference in New Issue
Block a user