2015-04-08 13:02:42 -06:00
|
|
|
#import "RCTConvert.h"
|
2015-03-30 23:07:55 -06:00
|
|
|
#import "RCTVideo.h"
|
2015-04-06 13:17:32 -06:00
|
|
|
#import "RCTBridgeModule.h"
|
|
|
|
#import "RCTEventDispatcher.h"
|
|
|
|
#import "UIView+React.h"
|
2015-03-30 23:07:55 -06:00
|
|
|
|
2015-04-08 00:49:14 -06:00
|
|
|
static NSString *const statusKeyPath = @"status";
|
2015-08-05 06:52:30 -06:00
|
|
|
static NSString *const playbackLikelyToKeepUpKeyPath = @"playbackLikelyToKeepUp";
|
2016-02-18 17:45:09 -07:00
|
|
|
static NSString *const playbackBufferEmptyKeyPath = @"playbackBufferEmpty";
|
2015-04-08 00:49:14 -06:00
|
|
|
|
2015-03-30 23:07:55 -06:00
|
|
|
@implementation RCTVideo
|
|
|
|
{
|
2015-04-07 16:24:49 -06:00
|
|
|
AVPlayer *_player;
|
2015-04-08 00:49:14 -06:00
|
|
|
AVPlayerItem *_playerItem;
|
2015-08-05 06:52:30 -06:00
|
|
|
BOOL _playerItemObserversSet;
|
2016-02-18 17:45:09 -07:00
|
|
|
BOOL _playerBufferEmpty;
|
2015-04-07 16:24:49 -06:00
|
|
|
AVPlayerLayer *_playerLayer;
|
2015-12-22 16:39:04 -07:00
|
|
|
AVPlayerViewController *_playerViewController;
|
2015-04-07 16:24:49 -06:00
|
|
|
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-08 16:15:57 -06:00
|
|
|
bool _pendingSeek;
|
|
|
|
float _pendingSeekTime;
|
|
|
|
float _lastSeekTime;
|
|
|
|
|
2015-04-07 16:24:49 -06:00
|
|
|
/* For sending videoProgress events */
|
2015-12-22 16:39:04 -07:00
|
|
|
Float64 _progressUpdateInterval;
|
|
|
|
BOOL _controls;
|
|
|
|
id _timeObserver;
|
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-05-10 17:01:25 -06:00
|
|
|
BOOL _paused;
|
2015-06-16 02:07:50 -06:00
|
|
|
BOOL _repeat;
|
|
|
|
NSString * _resizeMode;
|
2016-03-31 12:34:22 -06:00
|
|
|
BOOL _fullScreenPlayerPresented;
|
|
|
|
UIViewController * _presentingViewController;
|
2015-03-30 23:07:55 -06:00
|
|
|
}
|
|
|
|
|
2015-05-30 13:50:45 -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-06-16 22:14:14 -06:00
|
|
|
|
2015-04-08 11:34:27 -06:00
|
|
|
_rate = 1.0;
|
|
|
|
_volume = 1.0;
|
2015-06-16 22:14:14 -06:00
|
|
|
_resizeMode = @"AVLayerVideoGravityResizeAspectFill";
|
2015-04-08 16:15:57 -06:00
|
|
|
_pendingSeek = false;
|
|
|
|
_pendingSeekTime = 0.0f;
|
|
|
|
_lastSeekTime = 0.0f;
|
2015-12-22 16:39:04 -07:00
|
|
|
_progressUpdateInterval = 250;
|
|
|
|
_controls = NO;
|
2016-02-18 17:45:09 -07:00
|
|
|
_playerBufferEmpty = YES;
|
2015-06-25 16:40:46 -06:00
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
2015-08-05 06:52:30 -06:00
|
|
|
selector:@selector(applicationWillResignActive:)
|
|
|
|
name:UIApplicationWillResignActiveNotification
|
|
|
|
object:nil];
|
2015-06-25 16:40:46 -06:00
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
2015-08-05 06:52:30 -06:00
|
|
|
selector:@selector(applicationWillEnterForeground:)
|
|
|
|
name:UIApplicationWillEnterForegroundNotification
|
|
|
|
object:nil];
|
2015-03-30 23:07:55 -06:00
|
|
|
}
|
2015-04-18 12:21:12 -06:00
|
|
|
|
2015-03-30 23:07:55 -06:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-12-22 16:39:04 -07:00
|
|
|
- (AVPlayerViewController*)createPlayerViewController:(AVPlayer*)player withPlayerItem:(AVPlayerItem*)playerItem {
|
2016-03-31 15:10:25 -06:00
|
|
|
RCTVideoPlayerViewController* playerLayer= [[RCTVideoPlayerViewController alloc] init];
|
|
|
|
playerLayer.rctDelegate = self;
|
2015-12-22 16:39:04 -07:00
|
|
|
playerLayer.view.frame = self.bounds;
|
|
|
|
playerLayer.player = _player;
|
|
|
|
playerLayer.view.frame = self.bounds;
|
|
|
|
return playerLayer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------
|
|
|
|
** Get the duration for a AVPlayerItem.
|
|
|
|
** ------------------------------------------------------- */
|
|
|
|
|
|
|
|
- (CMTime)playerItemDuration
|
|
|
|
{
|
|
|
|
AVPlayerItem *playerItem = [_player currentItem];
|
|
|
|
if (playerItem.status == AVPlayerItemStatusReadyToPlay)
|
|
|
|
{
|
|
|
|
return([playerItem duration]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return(kCMTimeInvalid);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Cancels the previously registered time observer. */
|
|
|
|
-(void)removePlayerTimeObserver
|
|
|
|
{
|
|
|
|
if (_timeObserver)
|
|
|
|
{
|
|
|
|
[_player removeTimeObserver:_timeObserver];
|
|
|
|
_timeObserver = nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Progress
|
|
|
|
|
2015-06-26 12:41:41 -06:00
|
|
|
- (void)dealloc
|
|
|
|
{
|
2015-08-05 06:52:30 -06:00
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
2015-06-26 12:41:41 -06:00
|
|
|
}
|
|
|
|
|
2015-06-25 16:40:46 -06:00
|
|
|
#pragma mark - App lifecycle handlers
|
|
|
|
|
|
|
|
- (void)applicationWillResignActive:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
if (!_paused) {
|
2015-12-22 16:39:04 -07:00
|
|
|
[_player pause];
|
2015-07-20 10:46:49 -06:00
|
|
|
[_player setRate:0.0];
|
2015-06-25 16:40:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)applicationWillEnterForeground:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
[self applyModifiers];
|
|
|
|
}
|
|
|
|
|
2015-04-08 09:46:13 -06:00
|
|
|
#pragma mark - Progress
|
|
|
|
|
2015-05-30 13:50:45 -06:00
|
|
|
- (void)sendProgressUpdate
|
|
|
|
{
|
2015-12-22 16:39:04 -07:00
|
|
|
AVPlayerItem *video = [_player currentItem];
|
|
|
|
if (video == nil || video.status != AVPlayerItemStatusReadyToPlay) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CMTime playerDuration = [self playerItemDuration];
|
|
|
|
if (CMTIME_IS_INVALID(playerDuration)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CMTime currentTime = _player.currentTime;
|
|
|
|
const Float64 duration = CMTimeGetSeconds(playerDuration);
|
|
|
|
const Float64 currentTimeSecs = CMTimeGetSeconds(currentTime);
|
|
|
|
if( currentTimeSecs >= 0 && currentTimeSecs <= duration) {
|
|
|
|
[_eventDispatcher sendInputEventWithName:@"onVideoProgress"
|
|
|
|
body:@{
|
|
|
|
@"currentTime": [NSNumber numberWithFloat:CMTimeGetSeconds(currentTime)],
|
|
|
|
@"playableDuration": [self calculatePlayableDuration],
|
|
|
|
@"atValue": [NSNumber numberWithLongLong:currentTime.value],
|
|
|
|
@"atTimescale": [NSNumber numberWithInt:currentTime.timescale],
|
|
|
|
@"target": self.reactTag
|
|
|
|
}];
|
|
|
|
}
|
2015-04-06 14:05:05 -06:00
|
|
|
}
|
|
|
|
|
2015-06-10 15:29:38 -06:00
|
|
|
/*!
|
|
|
|
* Calculates and returns the playable duration of the current player item using its loaded time ranges.
|
|
|
|
*
|
2015-06-11 19:11:55 -06:00
|
|
|
* \returns The playable duration of the current player item in seconds.
|
2015-06-10 15:29:38 -06:00
|
|
|
*/
|
2015-08-05 06:52:30 -06:00
|
|
|
- (NSNumber *)calculatePlayableDuration
|
|
|
|
{
|
2015-06-10 15:29:38 -06:00
|
|
|
AVPlayerItem *video = _player.currentItem;
|
|
|
|
if (video.status == AVPlayerItemStatusReadyToPlay) {
|
|
|
|
__block CMTimeRange effectiveTimeRange;
|
|
|
|
[video.loadedTimeRanges enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
|
|
|
|
CMTimeRange timeRange = [obj CMTimeRangeValue];
|
|
|
|
if (CMTimeRangeContainsTime(timeRange, video.currentTime)) {
|
|
|
|
effectiveTimeRange = timeRange;
|
|
|
|
*stop = YES;
|
|
|
|
}
|
|
|
|
}];
|
2015-06-17 23:08:01 -06:00
|
|
|
Float64 playableDuration = CMTimeGetSeconds(CMTimeRangeGetEnd(effectiveTimeRange));
|
|
|
|
if (playableDuration > 0) {
|
|
|
|
return [NSNumber numberWithFloat:playableDuration];
|
|
|
|
}
|
2015-06-10 15:29:38 -06:00
|
|
|
}
|
2015-06-17 23:08:01 -06:00
|
|
|
return [NSNumber numberWithInteger:0];
|
2015-06-10 15:29:38 -06:00
|
|
|
}
|
|
|
|
|
2015-08-05 06:52:30 -06:00
|
|
|
- (void)addPlayerItemObservers
|
2015-05-30 13:47:14 -06:00
|
|
|
{
|
|
|
|
[_playerItem addObserver:self forKeyPath:statusKeyPath options:0 context:nil];
|
2016-02-18 17:45:09 -07:00
|
|
|
[_playerItem addObserver:self forKeyPath:playbackBufferEmptyKeyPath options:0 context:nil];
|
2015-08-05 06:52:30 -06:00
|
|
|
[_playerItem addObserver:self forKeyPath:playbackLikelyToKeepUpKeyPath options:0 context:nil];
|
|
|
|
_playerItemObserversSet = YES;
|
2015-05-30 13:47:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Fixes https://github.com/brentvatne/react-native-video/issues/43
|
|
|
|
* Crashes caused when trying to remove the observer when there is no
|
|
|
|
* observer set */
|
2015-08-05 06:52:30 -06:00
|
|
|
- (void)removePlayerItemObservers
|
2015-05-30 13:47:14 -06:00
|
|
|
{
|
2015-08-05 06:52:30 -06:00
|
|
|
if (_playerItemObserversSet) {
|
2015-05-30 13:47:14 -06:00
|
|
|
[_playerItem removeObserver:self forKeyPath:statusKeyPath];
|
2016-02-18 17:45:09 -07:00
|
|
|
[_playerItem removeObserver:self forKeyPath:playbackBufferEmptyKeyPath];
|
2015-08-05 06:52:30 -06:00
|
|
|
[_playerItem removeObserver:self forKeyPath:playbackLikelyToKeepUpKeyPath];
|
|
|
|
_playerItemObserversSet = NO;
|
2015-05-30 13:47:14 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 09:46:13 -06:00
|
|
|
#pragma mark - Player and source
|
|
|
|
|
2015-05-30 13:50:45 -06:00
|
|
|
- (void)setSrc:(NSDictionary *)source
|
|
|
|
{
|
2015-12-22 16:39:04 -07:00
|
|
|
[self removePlayerTimeObserver];
|
2015-08-05 06:52:30 -06:00
|
|
|
[self removePlayerItemObservers];
|
2015-04-08 09:46:13 -06:00
|
|
|
_playerItem = [self playerItemForSource:source];
|
2015-08-05 06:52:30 -06:00
|
|
|
[self addPlayerItemObservers];
|
2015-04-08 00:49:14 -06:00
|
|
|
|
|
|
|
[_player pause];
|
|
|
|
[_playerLayer removeFromSuperlayer];
|
2015-12-22 16:39:04 -07:00
|
|
|
_playerLayer = nil;
|
|
|
|
[_playerViewController.view removeFromSuperview];
|
|
|
|
_playerViewController = nil;
|
2015-04-08 00:49:14 -06:00
|
|
|
|
|
|
|
_player = [AVPlayer playerWithPlayerItem:_playerItem];
|
2015-04-04 18:55:37 -06:00
|
|
|
_player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
|
2015-04-08 00:49:14 -06:00
|
|
|
|
2015-12-22 16:39:04 -07:00
|
|
|
const Float64 progressUpdateIntervalMS = _progressUpdateInterval / 1000;
|
|
|
|
// @see endScrubbing in AVPlayerDemoPlaybackViewController.m of https://developer.apple.com/library/ios/samplecode/AVPlayerDemo/Introduction/Intro.html
|
|
|
|
__weak RCTVideo *weakSelf = self;
|
|
|
|
_timeObserver = [_player addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(progressUpdateIntervalMS, NSEC_PER_SEC)
|
|
|
|
queue:NULL
|
|
|
|
usingBlock:^(CMTime time) { [weakSelf sendProgressUpdate]; }
|
|
|
|
];
|
2015-08-22 13:42:26 -06:00
|
|
|
[_eventDispatcher sendInputEventWithName:@"onVideoLoadStart"
|
2015-08-05 06:52:30 -06:00
|
|
|
body:@{@"src": @{
|
|
|
|
@"uri": [source objectForKey:@"uri"],
|
|
|
|
@"type": [source objectForKey:@"type"],
|
|
|
|
@"isNetwork":[NSNumber numberWithBool:(bool)[source objectForKey:@"isNetwork"]]},
|
|
|
|
@"target": self.reactTag}];
|
2015-04-08 00:49:14 -06:00
|
|
|
}
|
2015-04-07 09:31:40 -06:00
|
|
|
|
2015-05-30 13:50:45 -06:00
|
|
|
- (AVPlayerItem*)playerItemForSource:(NSDictionary *)source
|
|
|
|
{
|
2015-04-08 11:34:27 -06:00
|
|
|
bool isNetwork = [RCTConvert BOOL:[source objectForKey:@"isNetwork"]];
|
|
|
|
bool isAsset = [RCTConvert BOOL:[source objectForKey:@"isAsset"]];
|
|
|
|
NSString *uri = [source objectForKey:@"uri"];
|
|
|
|
NSString *type = [source objectForKey:@"type"];
|
|
|
|
|
|
|
|
NSURL *url = (isNetwork || isAsset) ?
|
|
|
|
[NSURL URLWithString:uri] :
|
|
|
|
[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:uri ofType:type]];
|
|
|
|
|
|
|
|
if (isAsset) {
|
|
|
|
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
|
|
|
|
return [AVPlayerItem playerItemWithAsset:asset];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [AVPlayerItem playerItemWithURL:url];
|
|
|
|
}
|
|
|
|
|
2015-05-30 13:50:45 -06:00
|
|
|
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
|
|
|
|
{
|
2015-12-22 16:39:04 -07:00
|
|
|
if (object == _playerItem) {
|
2015-05-11 00:54:58 -06:00
|
|
|
|
2015-08-05 06:52:30 -06:00
|
|
|
if ([keyPath isEqualToString:statusKeyPath]) {
|
|
|
|
// Handle player item status change.
|
|
|
|
if (_playerItem.status == AVPlayerItemStatusReadyToPlay) {
|
|
|
|
float duration = CMTimeGetSeconds(_playerItem.asset.duration);
|
|
|
|
|
|
|
|
if (isnan(duration)) {
|
|
|
|
duration = 0.0;
|
|
|
|
}
|
|
|
|
|
2015-08-22 13:42:26 -06:00
|
|
|
[_eventDispatcher sendInputEventWithName:@"onVideoLoad"
|
2015-08-05 06:52:30 -06:00
|
|
|
body:@{@"duration": [NSNumber numberWithFloat:duration],
|
|
|
|
@"currentTime": [NSNumber numberWithFloat:CMTimeGetSeconds(_playerItem.currentTime)],
|
|
|
|
@"canPlayReverse": [NSNumber numberWithBool:_playerItem.canPlayReverse],
|
|
|
|
@"canPlayFastForward": [NSNumber numberWithBool:_playerItem.canPlayFastForward],
|
|
|
|
@"canPlaySlowForward": [NSNumber numberWithBool:_playerItem.canPlaySlowForward],
|
|
|
|
@"canPlaySlowReverse": [NSNumber numberWithBool:_playerItem.canPlaySlowReverse],
|
|
|
|
@"canStepBackward": [NSNumber numberWithBool:_playerItem.canStepBackward],
|
|
|
|
@"canStepForward": [NSNumber numberWithBool:_playerItem.canStepForward],
|
|
|
|
@"target": self.reactTag}];
|
|
|
|
|
|
|
|
[self attachListeners];
|
|
|
|
[self applyModifiers];
|
|
|
|
} else if(_playerItem.status == AVPlayerItemStatusFailed) {
|
2015-08-22 13:42:26 -06:00
|
|
|
[_eventDispatcher sendInputEventWithName:@"onVideoError"
|
2015-08-05 06:52:30 -06:00
|
|
|
body:@{@"error": @{
|
|
|
|
@"code": [NSNumber numberWithInteger: _playerItem.error.code],
|
|
|
|
@"domain": _playerItem.error.domain},
|
|
|
|
@"target": self.reactTag}];
|
|
|
|
}
|
2016-02-18 17:45:09 -07:00
|
|
|
} else if ([keyPath isEqualToString:playbackBufferEmptyKeyPath]) {
|
|
|
|
_playerBufferEmpty = YES;
|
2015-08-05 06:52:30 -06:00
|
|
|
} else if ([keyPath isEqualToString:playbackLikelyToKeepUpKeyPath]) {
|
|
|
|
// Continue playing (or not if paused) after being paused due to hitting an unbuffered zone.
|
2016-02-18 17:45:09 -07:00
|
|
|
if ((!_controls || _playerBufferEmpty) && _playerItem.playbackLikelyToKeepUp) {
|
2015-08-05 06:52:30 -06:00
|
|
|
[self setPaused:_paused];
|
2015-05-11 00:54:58 -06:00
|
|
|
}
|
2016-02-18 17:45:09 -07:00
|
|
|
_playerBufferEmpty = NO;
|
2015-04-08 00:49:14 -06:00
|
|
|
}
|
|
|
|
} else {
|
2015-12-22 16:39:04 -07:00
|
|
|
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
|
2015-04-08 00:49:14 -06:00
|
|
|
}
|
2015-03-30 23:07:55 -06:00
|
|
|
}
|
|
|
|
|
2015-05-30 13:50:45 -06:00
|
|
|
- (void)attachListeners
|
|
|
|
{
|
2015-07-18 06:38:56 -06:00
|
|
|
// listen for end of file
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(playerItemDidReachEnd:)
|
|
|
|
name:AVPlayerItemDidPlayToEndTimeNotification
|
|
|
|
object:[_player currentItem]];
|
2015-04-10 20:58:36 -06:00
|
|
|
}
|
|
|
|
|
2015-06-26 16:13:03 -06:00
|
|
|
- (void)playerItemDidReachEnd:(NSNotification *)notification
|
2015-05-30 13:50:45 -06:00
|
|
|
{
|
2015-08-22 13:42:26 -06:00
|
|
|
[_eventDispatcher sendInputEventWithName:@"onVideoEnd" body:@{@"target": self.reactTag}];
|
2015-08-05 06:52:30 -06:00
|
|
|
|
2015-06-24 20:09:05 -06:00
|
|
|
if (_repeat) {
|
2015-04-08 11:34:27 -06:00
|
|
|
AVPlayerItem *item = [notification object];
|
|
|
|
[item seekToTime:kCMTimeZero];
|
|
|
|
[self applyModifiers];
|
2015-06-24 20:09:05 -06:00
|
|
|
}
|
2015-04-08 09:46:13 -06:00
|
|
|
}
|
|
|
|
|
2015-04-08 11:34:27 -06:00
|
|
|
#pragma mark - Prop setters
|
2015-04-08 09:46:13 -06:00
|
|
|
|
2015-05-30 13:50:45 -06:00
|
|
|
- (void)setResizeMode:(NSString*)mode
|
|
|
|
{
|
2015-12-22 16:39:04 -07:00
|
|
|
if( _controls )
|
|
|
|
{
|
|
|
|
_playerViewController.videoGravity = mode;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_playerLayer.videoGravity = mode;
|
|
|
|
}
|
2015-06-16 02:07:50 -06:00
|
|
|
_resizeMode = mode;
|
2015-04-04 18:55:37 -06:00
|
|
|
}
|
|
|
|
|
2015-05-30 13:50:45 -06:00
|
|
|
- (void)setPaused:(BOOL)paused
|
|
|
|
{
|
2015-04-07 21:38:16 -06:00
|
|
|
if (paused) {
|
2015-12-22 16:39:04 -07:00
|
|
|
[_player pause];
|
2015-07-20 10:46:49 -06:00
|
|
|
[_player setRate:0.0];
|
2015-04-07 09:31:40 -06:00
|
|
|
} else {
|
2015-12-22 16:39:04 -07:00
|
|
|
[_player play];
|
2015-07-20 10:46:49 -06:00
|
|
|
[_player setRate:_rate];
|
2015-04-07 09:31:40 -06:00
|
|
|
}
|
2015-12-22 16:39:04 -07:00
|
|
|
|
2015-05-10 17:01:25 -06:00
|
|
|
_paused = paused;
|
2015-04-05 11:17:03 -06:00
|
|
|
}
|
|
|
|
|
2015-12-22 16:39:04 -07:00
|
|
|
- (float)getCurrentTime
|
|
|
|
{
|
|
|
|
return _playerItem != NULL ? CMTimeGetSeconds(_playerItem.currentTime) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setCurrentTime:(float)currentTime
|
|
|
|
{
|
|
|
|
[self setSeek: currentTime];
|
|
|
|
}
|
|
|
|
|
2015-05-30 13:50:45 -06:00
|
|
|
- (void)setSeek:(float)seekTime
|
|
|
|
{
|
2015-04-08 16:15:57 -06:00
|
|
|
int timeScale = 10000;
|
|
|
|
|
|
|
|
AVPlayerItem *item = _player.currentItem;
|
|
|
|
if (item && item.status == AVPlayerItemStatusReadyToPlay) {
|
|
|
|
// TODO check loadedTimeRanges
|
|
|
|
|
|
|
|
CMTime cmSeekTime = CMTimeMakeWithSeconds(seekTime, timeScale);
|
|
|
|
CMTime current = item.currentTime;
|
|
|
|
// TODO figure out a good tolerance level
|
|
|
|
CMTime tolerance = CMTimeMake(1000, timeScale);
|
2015-12-22 16:39:04 -07:00
|
|
|
|
2015-04-08 16:15:57 -06:00
|
|
|
if (CMTimeCompare(current, cmSeekTime) != 0) {
|
2015-07-18 06:38:56 -06:00
|
|
|
[_player seekToTime:cmSeekTime toleranceBefore:tolerance toleranceAfter:tolerance completionHandler:^(BOOL finished) {
|
2015-08-22 13:42:26 -06:00
|
|
|
[_eventDispatcher sendInputEventWithName:@"onVideoSeek"
|
2015-08-05 06:52:30 -06:00
|
|
|
body:@{@"currentTime": [NSNumber numberWithFloat:CMTimeGetSeconds(item.currentTime)],
|
|
|
|
@"seekTime": [NSNumber numberWithFloat:seekTime],
|
|
|
|
@"target": self.reactTag}];
|
2015-07-18 06:38:56 -06:00
|
|
|
}];
|
2015-05-30 13:50:45 -06:00
|
|
|
|
2015-04-09 08:19:09 -06:00
|
|
|
_pendingSeek = false;
|
2015-04-08 16:15:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2015-08-05 06:52:30 -06:00
|
|
|
// TODO: See if this makes sense and if so, actually implement it
|
2015-04-08 16:15:57 -06:00
|
|
|
_pendingSeek = true;
|
|
|
|
_pendingSeekTime = seekTime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-30 13:50:45 -06:00
|
|
|
- (void)setRate:(float)rate
|
|
|
|
{
|
2015-04-07 21:38:16 -06:00
|
|
|
_rate = rate;
|
|
|
|
[self applyModifiers];
|
|
|
|
}
|
|
|
|
|
2015-05-30 13:50:45 -06:00
|
|
|
- (void)setMuted:(BOOL)muted
|
|
|
|
{
|
2015-04-07 21:38:16 -06:00
|
|
|
_muted = muted;
|
|
|
|
[self applyModifiers];
|
|
|
|
}
|
|
|
|
|
2015-05-30 13:50:45 -06:00
|
|
|
- (void)setVolume:(float)volume
|
|
|
|
{
|
2015-04-07 21:38:16 -06:00
|
|
|
_volume = volume;
|
|
|
|
[self applyModifiers];
|
|
|
|
}
|
|
|
|
|
2015-05-30 13:50:45 -06:00
|
|
|
- (void)applyModifiers
|
|
|
|
{
|
2015-04-07 21:38:16 -06:00
|
|
|
if (_muted) {
|
2015-07-18 06:38:56 -06:00
|
|
|
[_player setVolume:0];
|
|
|
|
[_player setMuted:YES];
|
2015-04-07 21:38:16 -06:00
|
|
|
} else {
|
2015-07-18 06:38:56 -06:00
|
|
|
[_player setVolume:_volume];
|
|
|
|
[_player setMuted:NO];
|
2015-04-07 21:38:16 -06:00
|
|
|
}
|
|
|
|
|
2015-06-16 02:07:50 -06:00
|
|
|
[self setResizeMode:_resizeMode];
|
|
|
|
[self setRepeat:_repeat];
|
2015-05-10 17:01:25 -06:00
|
|
|
[self setPaused:_paused];
|
2015-12-22 16:39:04 -07:00
|
|
|
[self setControls:_controls];
|
2015-04-07 21:38:16 -06:00
|
|
|
}
|
2015-04-04 18:55:37 -06:00
|
|
|
|
2015-04-08 00:49:14 -06:00
|
|
|
- (void)setRepeat:(BOOL)repeat {
|
2015-06-16 02:07:50 -06:00
|
|
|
_repeat = repeat;
|
2015-03-31 00:29:15 -06:00
|
|
|
}
|
|
|
|
|
2016-03-31 12:34:22 -06:00
|
|
|
- (BOOL)getFullscreen
|
|
|
|
{
|
|
|
|
return _fullScreenPlayerPresented;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setFullscreen:(BOOL)fullscreen
|
|
|
|
{
|
|
|
|
if( fullscreen )
|
|
|
|
{
|
|
|
|
// Ensure player view controller is not null
|
|
|
|
if( !_playerViewController )
|
|
|
|
{
|
|
|
|
[self usePlayerViewController];
|
|
|
|
}
|
|
|
|
// Set presentation style to fullscreen
|
|
|
|
[_playerViewController setModalPresentationStyle:UIModalPresentationOverFullScreen];
|
|
|
|
|
|
|
|
// Find the nearest view controller
|
|
|
|
UIViewController *viewController = [self firstAvailableUIViewController];
|
|
|
|
if( !viewController )
|
|
|
|
{
|
|
|
|
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
|
|
|
|
viewController = keyWindow.rootViewController;
|
|
|
|
if( viewController.childViewControllers.count > 0 )
|
|
|
|
{
|
|
|
|
viewController = viewController.childViewControllers.lastObject;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( viewController )
|
|
|
|
{
|
|
|
|
_presentingViewController = viewController;
|
2016-03-31 13:35:10 -06:00
|
|
|
[_eventDispatcher sendInputEventWithName:@"onVideoFullscreenPlayerWillPresent" body:@{@"target": self.reactTag}];
|
2016-03-31 12:34:22 -06:00
|
|
|
[viewController presentViewController:_playerViewController animated:true completion:^{
|
|
|
|
_playerViewController.showsPlaybackControls = YES;
|
|
|
|
_fullScreenPlayerPresented = fullscreen;
|
2016-03-31 13:35:10 -06:00
|
|
|
[_eventDispatcher sendInputEventWithName:@"onVideoFullscreenPlayerDidPresent" body:@{@"target": self.reactTag}];
|
2016-03-31 12:34:22 -06:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-31 13:35:10 -06:00
|
|
|
[_eventDispatcher sendInputEventWithName:@"onVideoFullscreenPlayerWillDismiss" body:@{@"target": self.reactTag}];
|
2016-03-31 12:34:22 -06:00
|
|
|
[_presentingViewController dismissViewControllerAnimated:true completion:^{
|
|
|
|
_fullScreenPlayerPresented = fullscreen;
|
|
|
|
_presentingViewController = nil;
|
2016-03-31 15:10:25 -06:00
|
|
|
[self setControls:_controls];
|
2016-03-31 13:35:10 -06:00
|
|
|
[_eventDispatcher sendInputEventWithName:@"onVideoFullscreenPlayerDidDismiss" body:@{@"target": self.reactTag}];
|
2016-03-31 12:34:22 -06:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-22 16:39:04 -07:00
|
|
|
- (void)usePlayerViewController
|
|
|
|
{
|
|
|
|
if( _player )
|
|
|
|
{
|
|
|
|
_playerViewController = [self createPlayerViewController:_player withPlayerItem:_playerItem];
|
|
|
|
[self addSubview:_playerViewController.view];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)usePlayerLayer
|
|
|
|
{
|
|
|
|
if( _player )
|
|
|
|
{
|
|
|
|
_playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
|
|
|
|
_playerLayer.frame = self.bounds;
|
|
|
|
_playerLayer.needsDisplayOnBoundsChange = YES;
|
|
|
|
|
|
|
|
[self.layer addSublayer:_playerLayer];
|
|
|
|
self.layer.needsDisplayOnBoundsChange = YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setControls:(BOOL)controls
|
|
|
|
{
|
|
|
|
if( _controls != controls || (!_playerLayer && !_playerViewController) )
|
|
|
|
{
|
|
|
|
_controls = controls;
|
|
|
|
if( _controls )
|
|
|
|
{
|
|
|
|
[_playerLayer removeFromSuperlayer];
|
|
|
|
_playerLayer = nil;
|
|
|
|
[self usePlayerViewController];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[_playerViewController.view removeFromSuperview];
|
|
|
|
_playerViewController = nil;
|
|
|
|
[self usePlayerLayer];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 11:34:27 -06:00
|
|
|
#pragma mark - React View Management
|
2015-04-08 09:46:13 -06:00
|
|
|
|
2015-05-30 13:50:45 -06:00
|
|
|
- (void)insertReactSubview:(UIView *)view atIndex:(NSInteger)atIndex
|
|
|
|
{
|
2015-12-22 16:39:04 -07:00
|
|
|
// We are early in the game and somebody wants to set a subview.
|
|
|
|
// That can only be in the context of playerViewController.
|
|
|
|
if( !_controls && !_playerLayer && !_playerViewController )
|
|
|
|
{
|
|
|
|
[self setControls:true];
|
|
|
|
}
|
|
|
|
|
|
|
|
if( _controls )
|
|
|
|
{
|
|
|
|
view.frame = self.bounds;
|
|
|
|
[_playerViewController.contentOverlayView insertSubview:view atIndex:atIndex];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RCTLogError(@"video cannot have any subviews");
|
|
|
|
}
|
2015-03-30 23:07:55 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-30 13:50:45 -06:00
|
|
|
- (void)removeReactSubview:(UIView *)subview
|
|
|
|
{
|
2015-12-22 16:39:04 -07:00
|
|
|
if( _controls )
|
|
|
|
{
|
|
|
|
[subview removeFromSuperview];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RCTLogError(@"video cannot have any subviews");
|
|
|
|
}
|
2015-03-30 23:07:55 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-30 13:50:45 -06:00
|
|
|
- (void)layoutSubviews
|
|
|
|
{
|
2015-03-30 23:07:55 -06:00
|
|
|
[super layoutSubviews];
|
2015-12-22 16:39:04 -07:00
|
|
|
if( _controls )
|
|
|
|
{
|
|
|
|
_playerViewController.view.frame = self.bounds;
|
|
|
|
|
|
|
|
// also adjust all subviews of contentOverlayView
|
|
|
|
for (UIView* subview in _playerViewController.contentOverlayView.subviews) {
|
|
|
|
subview.frame = self.bounds;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[CATransaction begin];
|
|
|
|
[CATransaction setAnimationDuration:0];
|
|
|
|
_playerLayer.frame = self.bounds;
|
|
|
|
[CATransaction commit];
|
|
|
|
}
|
2015-04-04 18:55:37 -06:00
|
|
|
}
|
|
|
|
|
2015-04-08 11:34:27 -06:00
|
|
|
#pragma mark - Lifecycle
|
2015-04-08 09:46:13 -06:00
|
|
|
|
2015-05-30 13:50:45 -06:00
|
|
|
- (void)removeFromSuperview
|
|
|
|
{
|
2015-04-10 01:15:19 -06:00
|
|
|
[_player pause];
|
|
|
|
_player = nil;
|
|
|
|
|
|
|
|
[_playerLayer removeFromSuperlayer];
|
|
|
|
_playerLayer = nil;
|
2015-12-22 16:39:04 -07:00
|
|
|
|
|
|
|
[_playerViewController.view removeFromSuperview];
|
|
|
|
_playerViewController = nil;
|
2015-04-10 01:15:19 -06:00
|
|
|
|
2015-12-22 16:39:04 -07:00
|
|
|
[self removePlayerTimeObserver];
|
2015-08-05 06:52:30 -06:00
|
|
|
[self removePlayerItemObservers];
|
2015-04-10 01:15:19 -06:00
|
|
|
|
|
|
|
_eventDispatcher = nil;
|
2015-04-04 18:55:37 -06:00
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
2015-06-24 22:26:36 -06:00
|
|
|
|
|
|
|
[super removeFromSuperview];
|
2015-03-30 23:07:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|