2015-03-30 23:07:55 -06:00
|
|
|
#import "RCTVideo.h"
|
|
|
|
#import "RCTLog.h"
|
2015-04-06 13:17:32 -06:00
|
|
|
#import "RCTBridgeModule.h"
|
|
|
|
#import "RCTEventDispatcher.h"
|
|
|
|
#import "UIView+React.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-07 16:24:49 -06:00
|
|
|
AVPlayer *_player;
|
|
|
|
AVPlayerLayer *_playerLayer;
|
|
|
|
NSURL *_videoURL;
|
2015-04-06 14:05:05 -06:00
|
|
|
|
2015-04-07 16:24:49 -06:00
|
|
|
/* Required to publish events */
|
|
|
|
RCTEventDispatcher *_eventDispatcher;
|
2015-04-06 14:05:05 -06:00
|
|
|
|
2015-04-07 16:24:49 -06:00
|
|
|
/* For sending videoProgress events */
|
|
|
|
id _progressUpdateTimer;
|
|
|
|
int _progressUpdateInterval;
|
|
|
|
NSDate *_prevProgressUpdateTime;
|
2015-04-07 21:38:16 -06:00
|
|
|
|
|
|
|
/* Keep track of any modifiers, need to be applied after each play */
|
|
|
|
float _volume;
|
|
|
|
float _rate;
|
|
|
|
BOOL _muted;
|
2015-03-30 23:07:55 -06:00
|
|
|
}
|
|
|
|
|
2015-04-06 13:17:32 -06:00
|
|
|
- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher
|
2015-03-30 23:07:55 -06:00
|
|
|
{
|
|
|
|
if ((self = [super init])) {
|
2015-04-06 13:17:32 -06:00
|
|
|
_eventDispatcher = eventDispatcher;
|
2015-04-06 14:05:05 -06:00
|
|
|
|
|
|
|
/* Initialize videoProgress status publisher */
|
2015-04-06 14:26:11 -06:00
|
|
|
_progressUpdateInterval = 250;
|
2015-04-06 14:05:05 -06:00
|
|
|
_prevProgressUpdateTime = nil;
|
|
|
|
_progressUpdateTimer = [CADisplayLink displayLinkWithTarget:self selector:@selector(sendProgressUpdate)];
|
|
|
|
[_progressUpdateTimer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
|
2015-03-30 23:07:55 -06:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-04-06 14:05:05 -06:00
|
|
|
- (void)sendProgressUpdate
|
|
|
|
{
|
|
|
|
AVPlayerItem *video = [_player currentItem];
|
|
|
|
if (video == nil) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-06 14:26:11 -06:00
|
|
|
if (_prevProgressUpdateTime == nil ||
|
|
|
|
(([_prevProgressUpdateTime timeIntervalSinceNow] * -1000.0) >= _progressUpdateInterval)) {
|
2015-04-06 14:05:05 -06:00
|
|
|
[_eventDispatcher sendInputEventWithName:@"videoProgress" body:@{
|
|
|
|
@"currentTime": [NSNumber numberWithFloat:CMTimeGetSeconds(video.currentTime)],
|
|
|
|
@"target": self.reactTag
|
|
|
|
}];
|
|
|
|
_prevProgressUpdateTime = [NSDate date];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-30 23:07:55 -06:00
|
|
|
- (void)setSrc:(NSString *)source
|
|
|
|
{
|
2015-04-07 13:24:52 -06:00
|
|
|
BOOL isHttpPrefix = [source hasPrefix:@"http://"];
|
|
|
|
if (isHttpPrefix) {
|
|
|
|
_videoURL = [NSURL URLWithString:source];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
_videoURL = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:source ofType:@"mp4"]];
|
|
|
|
}
|
2015-04-06 13:17:32 -06:00
|
|
|
_player = [AVPlayer playerWithURL:_videoURL];
|
2015-04-04 18:55:37 -06:00
|
|
|
_player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
|
|
|
|
_playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
|
|
|
|
_playerLayer.frame = self.bounds;
|
|
|
|
_playerLayer.needsDisplayOnBoundsChange = YES;
|
2015-04-06 13:17:32 -06:00
|
|
|
|
2015-04-04 18:55:37 -06:00
|
|
|
[self.layer addSublayer:_playerLayer];
|
|
|
|
self.layer.needsDisplayOnBoundsChange = YES;
|
2015-04-06 13:17:32 -06:00
|
|
|
|
|
|
|
AVPlayerItem *video = [_player currentItem];
|
|
|
|
|
|
|
|
[_eventDispatcher sendInputEventWithName:@"videoLoaded" body:@{
|
|
|
|
@"duration": [NSNumber numberWithFloat:CMTimeGetSeconds(video.asset.duration)],
|
|
|
|
@"currentTime": [NSNumber numberWithFloat:CMTimeGetSeconds(video.currentTime)],
|
|
|
|
@"canPlayReverse": [NSNumber numberWithBool:video.canPlayReverse],
|
|
|
|
@"canPlayFastForward": [NSNumber numberWithBool:video.canPlayFastForward],
|
|
|
|
@"canPlaySlowForward": [NSNumber numberWithBool:video.canPlaySlowForward],
|
|
|
|
@"canPlaySlowReverse": [NSNumber numberWithBool:video.canPlaySlowReverse],
|
|
|
|
@"canStepBackward": [NSNumber numberWithBool:video.canStepBackward],
|
|
|
|
@"canStepForward": [NSNumber numberWithBool:video.canStepForward],
|
|
|
|
@"target": self.reactTag
|
|
|
|
}];
|
2015-04-07 09:31:40 -06:00
|
|
|
|
|
|
|
[_player play];
|
2015-04-07 21:38:16 -06:00
|
|
|
|
|
|
|
/* rate and volume must be set after play is called */
|
|
|
|
[self applyModifiers];
|
2015-03-30 23:07:55 -06:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-04-07 21:38:16 -06:00
|
|
|
- (void)setPaused:(BOOL)paused
|
2015-04-05 11:17:03 -06:00
|
|
|
{
|
2015-04-07 21:38:16 -06:00
|
|
|
if (paused) {
|
2015-04-07 09:31:40 -06:00
|
|
|
[_player pause];
|
|
|
|
} else {
|
|
|
|
[_player play];
|
|
|
|
}
|
2015-04-05 11:17:03 -06:00
|
|
|
}
|
|
|
|
|
2015-04-07 21:38:16 -06:00
|
|
|
- (void)setRate:(float)rate
|
|
|
|
{
|
|
|
|
_rate = rate;
|
|
|
|
[self applyModifiers];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setMuted:(BOOL)muted
|
|
|
|
{
|
|
|
|
_muted = muted;
|
|
|
|
[self applyModifiers];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setVolume:(float)volume {
|
|
|
|
_volume = volume;
|
|
|
|
[self applyModifiers];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)applyModifiers
|
|
|
|
{
|
|
|
|
/* volume must be set to 0 if muted is YES, or the video seems to
|
|
|
|
* freeze */
|
|
|
|
if (_muted) {
|
|
|
|
[_player setVolume:0];
|
|
|
|
[_player setMuted:YES];
|
|
|
|
} else {
|
|
|
|
[_player setVolume:_volume];
|
|
|
|
[_player setMuted:NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
[_player setRate:_rate];
|
|
|
|
}
|
2015-04-04 18:55:37 -06:00
|
|
|
|
|
|
|
- (void)playerItemDidReachEnd:(NSNotification *)notification {
|
|
|
|
AVPlayerItem *item = [notification object];
|
|
|
|
[item seekToTime:kCMTimeZero];
|
|
|
|
[_player play];
|
2015-04-07 21:38:16 -06:00
|
|
|
[self applyModifiers];
|
2015-04-04 18:55:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (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;
|
|
|
|
}
|
|
|
|
|
2015-04-07 11:41:34 -06:00
|
|
|
- (void)removeFromSuperview
|
2015-04-04 18:55:37 -06:00
|
|
|
{
|
2015-04-07 11:41:34 -06:00
|
|
|
[_player pause];
|
2015-04-06 14:05:05 -06:00
|
|
|
[_progressUpdateTimer invalidate];
|
2015-04-07 11:41:34 -06:00
|
|
|
[_playerLayer removeFromSuperlayer];
|
|
|
|
_player = nil;
|
|
|
|
_prevProgressUpdateTime = nil;
|
|
|
|
_eventDispatcher = nil;
|
2015-04-04 18:55:37 -06:00
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
2015-03-30 23:07:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|