2015-03-30 23:07:55 -06:00
|
|
|
#import "RCTVideo.h"
|
|
|
|
#import "RCTLog.h"
|
2015-04-04 18:55:37 -06:00
|
|
|
#import <AVFoundation/AVFoundation.h>
|
2015-03-30 23:07:55 -06:00
|
|
|
|
|
|
|
@implementation RCTVideo
|
|
|
|
{
|
2015-04-04 18:55:37 -06:00
|
|
|
AVPlayer *_player;
|
|
|
|
AVPlayerLayer *_playerLayer;
|
2015-03-30 23:07:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id)init
|
|
|
|
{
|
|
|
|
if ((self = [super init])) {
|
2015-04-04 18:55:37 -06:00
|
|
|
|
2015-03-30 23:07:55 -06:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setSrc:(NSString *)source
|
|
|
|
{
|
|
|
|
NSURL *videoURL = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:source ofType:@"mp4"]];
|
2015-04-04 18:55:37 -06:00
|
|
|
_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;
|
2015-03-30 23:07:55 -06:00
|
|
|
[_player play];
|
|
|
|
}
|
|
|
|
|
2015-04-04 18:55:37 -06:00
|
|
|
- (void)setResizeMode:(NSString*)mode
|
2015-03-30 23:07:55 -06:00
|
|
|
{
|
2015-04-04 18:55:37 -06:00
|
|
|
_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];
|
2015-03-30 23:07:55 -06:00
|
|
|
}
|
|
|
|
|
2015-03-31 00:29:15 -06:00
|
|
|
- (void)setRepeat:(BOOL)repeat
|
|
|
|
{
|
|
|
|
if (repeat) {
|
2015-04-04 18:55:37 -06:00
|
|
|
[self setRepeatEnabled];
|
2015-03-31 00:29:15 -06:00
|
|
|
} else {
|
2015-04-04 18:55:37 -06:00
|
|
|
[self setRepeatDisabled];
|
2015-03-31 00:29:15 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-30 23:07:55 -06:00
|
|
|
- (void)insertReactSubview:(UIView *)view atIndex:(NSInteger)atIndex
|
|
|
|
{
|
|
|
|
RCTLogError(@"video cannot have any subviews");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)removeReactSubview:(UIView *)subview
|
|
|
|
{
|
|
|
|
RCTLogError(@"video cannot have any subviews");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)layoutSubviews
|
|
|
|
{
|
|
|
|
[super layoutSubviews];
|
2015-04-04 18:55:37 -06:00
|
|
|
_playerLayer.frame = self.bounds;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
2015-03-30 23:07:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|