2017-01-11 05:43:43 -07:00
|
|
|
#import <React/RCTConvert.h>
|
2015-03-30 23:07:55 -06:00
|
|
|
#import "RCTVideo.h"
|
2017-01-11 05:43:43 -07:00
|
|
|
#import <React/RCTBridgeModule.h>
|
|
|
|
#import <React/RCTEventDispatcher.h>
|
|
|
|
#import <React/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";
|
2016-04-28 06:25:45 -06:00
|
|
|
static NSString *const readyForDisplayKeyPath = @"readyForDisplay";
|
2016-04-28 06:29:09 -06:00
|
|
|
static NSString *const playbackRate = @"rate";
|
2017-02-13 19:38:02 -07:00
|
|
|
static NSString *const timedMetadata = @"timedMetadata";
|
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;
|
2018-06-20 17:33:50 -06:00
|
|
|
BOOL _playerLayerObserverSet;
|
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;
|
2016-05-17 01:37:11 -06:00
|
|
|
BOOL _playbackRateObserverRegistered;
|
2018-06-25 12:43:51 -06:00
|
|
|
BOOL _videoLoadStarted;
|
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;
|
2018-06-05 19:40:12 -06:00
|
|
|
BOOL _allowsExternalPlayback;
|
2018-06-02 03:24:13 -06:00
|
|
|
NSDictionary * _selectedTextTrack;
|
2016-04-28 06:38:21 -06:00
|
|
|
BOOL _playbackStalled;
|
2015-10-30 03:34:54 -06:00
|
|
|
BOOL _playInBackground;
|
2016-04-29 05:55:34 -06:00
|
|
|
BOOL _playWhenInactive;
|
2017-04-20 12:10:06 -06:00
|
|
|
NSString * _ignoreSilentSwitch;
|
2015-06-16 02:07:50 -06:00
|
|
|
NSString * _resizeMode;
|
2016-04-01 06:55:23 -06:00
|
|
|
BOOL _fullscreenPlayerPresented;
|
2016-03-31 12:34:22 -06:00
|
|
|
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
|
|
|
|
2016-05-17 01:37:11 -06:00
|
|
|
_playbackRateObserverRegistered = NO;
|
2016-04-28 06:38:21 -06:00
|
|
|
_playbackStalled = NO;
|
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;
|
2016-06-01 23:58:38 -06:00
|
|
|
_playInBackground = false;
|
2018-06-10 10:04:13 -06:00
|
|
|
_allowsExternalPlayback = YES;
|
2016-04-29 05:55:34 -06:00
|
|
|
_playWhenInactive = false;
|
2017-04-20 12:10:06 -06:00
|
|
|
_ignoreSilentSwitch = @"inherit"; // inherit, ignore, obey
|
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
|
|
|
|
2016-04-29 01:46:28 -06:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(applicationDidEnterBackground:)
|
|
|
|
name:UIApplicationDidEnterBackgroundNotification
|
|
|
|
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];
|
2016-04-01 05:04:16 -06:00
|
|
|
playerLayer.showsPlaybackControls = NO;
|
2016-03-31 15:10:25 -06:00
|
|
|
playerLayer.rctDelegate = self;
|
2015-12-22 16:39:04 -07:00
|
|
|
playerLayer.view.frame = self.bounds;
|
2018-02-27 19:15:42 -07:00
|
|
|
playerLayer.player = player;
|
2015-12-22 16:39:04 -07:00
|
|
|
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]);
|
|
|
|
}
|
2016-08-04 06:32:04 -06:00
|
|
|
|
2015-12-22 16:39:04 -07:00
|
|
|
return(kCMTimeInvalid);
|
|
|
|
}
|
|
|
|
|
2016-09-06 20:23:13 -06:00
|
|
|
- (CMTimeRange)playerItemSeekableTimeRange
|
|
|
|
{
|
|
|
|
AVPlayerItem *playerItem = [_player currentItem];
|
|
|
|
if (playerItem.status == AVPlayerItemStatusReadyToPlay)
|
|
|
|
{
|
|
|
|
return [playerItem seekableTimeRanges].firstObject.CMTimeRangeValue;
|
|
|
|
}
|
2017-04-20 12:10:06 -06:00
|
|
|
|
2016-09-06 20:23:13 -06:00
|
|
|
return (kCMTimeRangeZero);
|
|
|
|
}
|
|
|
|
|
2017-09-28 19:37:26 -06:00
|
|
|
-(void)addPlayerTimeObserver
|
2018-05-15 23:19:12 -06:00
|
|
|
{
|
2017-09-28 19:37:26 -06:00
|
|
|
const Float64 progressUpdateIntervalMS = _progressUpdateInterval / 1000;
|
2018-05-29 17:11:15 -06:00
|
|
|
// @see endScrubbing in AVPlayerDemoPlaybackViewController.m
|
|
|
|
// of https://developer.apple.com/library/ios/samplecode/AVPlayerDemo/Introduction/Intro.html
|
2017-09-28 19:37:26 -06:00
|
|
|
__weak RCTVideo *weakSelf = self;
|
|
|
|
_timeObserver = [_player addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(progressUpdateIntervalMS, NSEC_PER_SEC)
|
|
|
|
queue:NULL
|
|
|
|
usingBlock:^(CMTime time) { [weakSelf sendProgressUpdate]; }
|
|
|
|
];
|
2018-05-15 23:19:12 -06:00
|
|
|
}
|
2015-12-22 16:39:04 -07:00
|
|
|
|
|
|
|
/* 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];
|
2017-06-09 15:31:46 -06:00
|
|
|
[self removePlayerLayer];
|
2018-03-19 13:56:55 -06:00
|
|
|
[self removePlayerItemObservers];
|
2016-08-04 06:32:04 -06:00
|
|
|
[_player removeObserver:self forKeyPath:playbackRate context:nil];
|
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
|
|
|
|
{
|
2016-04-29 05:55:34 -06:00
|
|
|
if (_playInBackground || _playWhenInactive || _paused) return;
|
|
|
|
|
|
|
|
[_player pause];
|
|
|
|
[_player setRate:0.0];
|
2015-06-25 16:40:46 -06:00
|
|
|
}
|
|
|
|
|
2016-04-29 01:46:28 -06:00
|
|
|
- (void)applicationDidEnterBackground:(NSNotification *)notification
|
|
|
|
{
|
2016-06-01 23:58:38 -06:00
|
|
|
if (_playInBackground) {
|
|
|
|
// Needed to play sound in background. See https://developer.apple.com/library/ios/qa/qa1668/_index.html
|
|
|
|
[_playerLayer setPlayer:nil];
|
|
|
|
}
|
2016-04-29 01:46:28 -06:00
|
|
|
}
|
|
|
|
|
2015-06-25 16:40:46 -06:00
|
|
|
- (void)applicationWillEnterForeground:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
[self applyModifiers];
|
2016-04-28 07:06:22 -06:00
|
|
|
if (_playInBackground) {
|
|
|
|
[_playerLayer setPlayer:_player];
|
|
|
|
}
|
2015-06-25 16:40:46 -06:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2016-08-04 06:32:04 -06:00
|
|
|
|
2015-12-22 16:39:04 -07:00
|
|
|
CMTime playerDuration = [self playerItemDuration];
|
|
|
|
if (CMTIME_IS_INVALID(playerDuration)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CMTime currentTime = _player.currentTime;
|
|
|
|
const Float64 duration = CMTimeGetSeconds(playerDuration);
|
|
|
|
const Float64 currentTimeSecs = CMTimeGetSeconds(currentTime);
|
2017-11-18 15:10:58 -07:00
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:@"RCTVideo_progress" object:nil userInfo:@{@"progress": [NSNumber numberWithDouble: currentTimeSecs / duration]}];
|
|
|
|
|
2017-01-16 09:27:08 -07:00
|
|
|
if( currentTimeSecs >= 0 && self.onVideoProgress) {
|
2016-12-12 17:16:11 -07:00
|
|
|
self.onVideoProgress(@{
|
|
|
|
@"currentTime": [NSNumber numberWithFloat:CMTimeGetSeconds(currentTime)],
|
|
|
|
@"playableDuration": [self calculatePlayableDuration],
|
|
|
|
@"atValue": [NSNumber numberWithLongLong:currentTime.value],
|
|
|
|
@"atTimescale": [NSNumber numberWithInt:currentTime.timescale],
|
|
|
|
@"target": self.reactTag,
|
2017-09-06 19:12:34 -06:00
|
|
|
@"seekableDuration": [self calculateSeekableDuration],
|
2016-12-12 17:16:11 -07:00
|
|
|
});
|
2015-12-22 16:39:04 -07:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2017-09-06 19:12:34 -06:00
|
|
|
- (NSNumber *)calculateSeekableDuration
|
|
|
|
{
|
|
|
|
CMTimeRange timeRange = [self playerItemSeekableTimeRange];
|
|
|
|
if (CMTIME_IS_NUMERIC(timeRange.duration))
|
|
|
|
{
|
|
|
|
return [NSNumber numberWithFloat:CMTimeGetSeconds(timeRange.duration)];
|
|
|
|
}
|
|
|
|
return [NSNumber numberWithInteger:0];
|
|
|
|
}
|
|
|
|
|
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];
|
2017-02-13 19:38:02 -07:00
|
|
|
[_playerItem addObserver:self forKeyPath:timedMetadata options:NSKeyValueObservingOptionNew context:nil];
|
2015-08-05 06:52:30 -06:00
|
|
|
_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];
|
2017-02-13 19:38:02 -07:00
|
|
|
[_playerItem removeObserver:self forKeyPath:timedMetadata];
|
2015-08-05 06:52:30 -06:00
|
|
|
_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
|
|
|
|
{
|
2018-03-19 13:56:55 -06:00
|
|
|
[self removePlayerLayer];
|
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];
|
2015-12-22 16:39:04 -07:00
|
|
|
[_playerViewController.view removeFromSuperview];
|
|
|
|
_playerViewController = nil;
|
2015-04-08 00:49:14 -06:00
|
|
|
|
2016-05-17 01:40:26 -06:00
|
|
|
if (_playbackRateObserverRegistered) {
|
2016-05-19 12:27:23 -06:00
|
|
|
[_player removeObserver:self forKeyPath:playbackRate context:nil];
|
2016-05-17 01:40:26 -06:00
|
|
|
_playbackRateObserverRegistered = NO;
|
|
|
|
}
|
2016-05-27 02:56:16 -06:00
|
|
|
|
2015-04-08 00:49:14 -06:00
|
|
|
_player = [AVPlayer playerWithPlayerItem:_playerItem];
|
2015-04-04 18:55:37 -06:00
|
|
|
_player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
|
2016-08-04 06:32:04 -06:00
|
|
|
|
2016-04-28 06:29:09 -06:00
|
|
|
[_player addObserver:self forKeyPath:playbackRate options:0 context:nil];
|
2016-05-17 01:38:35 -06:00
|
|
|
_playbackRateObserverRegistered = YES;
|
2015-04-08 00:49:14 -06:00
|
|
|
|
2018-05-15 23:19:12 -06:00
|
|
|
[self addPlayerTimeObserver];
|
2016-12-12 17:16:11 -07:00
|
|
|
|
|
|
|
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
2018-06-25 12:43:51 -06:00
|
|
|
// Perform on next run loop, otherwise onVideoLoadStart is nil
|
|
|
|
if (self.onVideoLoadStart) {
|
2017-01-28 18:46:30 -07:00
|
|
|
id uri = [source objectForKey:@"uri"];
|
|
|
|
id type = [source objectForKey:@"type"];
|
|
|
|
self.onVideoLoadStart(@{@"src": @{
|
|
|
|
@"uri": uri ? uri : [NSNull null],
|
|
|
|
@"type": type ? type : [NSNull null],
|
|
|
|
@"isNetwork": [NSNumber numberWithBool:(bool)[source objectForKey:@"isNetwork"]]},
|
|
|
|
@"target": self.reactTag
|
|
|
|
});
|
|
|
|
}
|
2016-12-12 17:16:11 -07:00
|
|
|
});
|
2018-06-25 12:43:51 -06:00
|
|
|
_videoLoadStarted = YES;
|
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"];
|
2017-10-02 12:11:41 -06:00
|
|
|
NSDictionary *headers = [source objectForKey:@"requestHeaders"];
|
|
|
|
|
2015-04-08 11:34:27 -06:00
|
|
|
NSURL *url = (isNetwork || isAsset) ?
|
|
|
|
[NSURL URLWithString:uri] :
|
|
|
|
[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:uri ofType:type]];
|
|
|
|
|
2016-10-06 15:34:01 -06:00
|
|
|
if (isNetwork) {
|
2018-06-09 13:53:21 -06:00
|
|
|
NSMutableDictionary *assetOptions = [[NSMutableDictionary alloc]init];
|
2018-06-26 16:39:04 -06:00
|
|
|
/* Per #1091, this is not a public API. We need to either get approval from Apple to use this
|
|
|
|
* or use a different approach.
|
2017-10-02 12:11:41 -06:00
|
|
|
if ([headers count] > 0) {
|
2018-06-11 03:10:01 -06:00
|
|
|
[assetOptions setObject:headers forKey:@"AVURLAssetHTTPHeaderFieldsKey"];
|
2017-10-02 12:11:41 -06:00
|
|
|
}
|
2018-06-26 16:39:04 -06:00
|
|
|
*/
|
2016-10-06 15:34:01 -06:00
|
|
|
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
|
2018-06-11 03:10:01 -06:00
|
|
|
[assetOptions setObject:cookies forKey:AVURLAssetHTTPCookiesKey];
|
2018-06-09 13:53:21 -06:00
|
|
|
|
|
|
|
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:assetOptions];
|
2016-10-06 15:34:01 -06:00
|
|
|
return [AVPlayerItem playerItemWithAsset:asset];
|
|
|
|
}
|
|
|
|
else if (isAsset) {
|
2015-04-08 11:34:27 -06:00
|
|
|
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
|
|
|
|
{
|
2018-06-25 12:43:51 -06:00
|
|
|
if (object == _playerItem) {
|
2017-02-13 19:38:02 -07:00
|
|
|
// When timeMetadata is read the event onTimedMetadata is triggered
|
2018-06-25 12:43:51 -06:00
|
|
|
if ([keyPath isEqualToString:timedMetadata]) {
|
|
|
|
NSArray<AVMetadataItem *> *items = [change objectForKey:@"new"];
|
|
|
|
if (items && ![items isEqual:[NSNull null]] && items.count > 0) {
|
|
|
|
NSMutableArray *array = [NSMutableArray new];
|
|
|
|
for (AVMetadataItem *item in items) {
|
|
|
|
NSString *value = item.value;
|
|
|
|
NSString *identifier = item.identifier;
|
|
|
|
|
|
|
|
if (![value isEqual: [NSNull null]]) {
|
|
|
|
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjects:@[value, identifier] forKeys:@[@"value", @"identifier"]];
|
|
|
|
|
|
|
|
[array addObject:dictionary];
|
|
|
|
}
|
2017-02-13 19:38:02 -07:00
|
|
|
}
|
2018-06-25 12:43:51 -06:00
|
|
|
|
|
|
|
self.onTimedMetadata(@{
|
|
|
|
@"target": self.reactTag,
|
|
|
|
@"metadata": array
|
|
|
|
});
|
|
|
|
}
|
2017-02-13 19:38:02 -07:00
|
|
|
}
|
2018-06-25 12:43:51 -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);
|
2018-06-25 12:43:51 -06:00
|
|
|
|
2015-08-05 06:52:30 -06:00
|
|
|
if (isnan(duration)) {
|
|
|
|
duration = 0.0;
|
|
|
|
}
|
2018-06-25 12:43:51 -06:00
|
|
|
|
2016-04-08 03:10:22 -06:00
|
|
|
NSObject *width = @"undefined";
|
|
|
|
NSObject *height = @"undefined";
|
2016-05-01 08:41:30 -06:00
|
|
|
NSString *orientation = @"undefined";
|
2018-06-25 12:43:51 -06:00
|
|
|
|
2016-04-08 03:10:22 -06:00
|
|
|
if ([_playerItem.asset tracksWithMediaType:AVMediaTypeVideo].count > 0) {
|
2016-05-01 09:26:56 -06:00
|
|
|
AVAssetTrack *videoTrack = [[_playerItem.asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
|
|
|
|
width = [NSNumber numberWithFloat:videoTrack.naturalSize.width];
|
|
|
|
height = [NSNumber numberWithFloat:videoTrack.naturalSize.height];
|
|
|
|
CGAffineTransform preferredTransform = [videoTrack preferredTransform];
|
2018-06-25 12:43:51 -06:00
|
|
|
|
2016-05-01 09:26:56 -06:00
|
|
|
if ((videoTrack.naturalSize.width == preferredTransform.tx
|
2018-06-25 12:43:51 -06:00
|
|
|
&& videoTrack.naturalSize.height == preferredTransform.ty)
|
|
|
|
|| (preferredTransform.tx == 0 && preferredTransform.ty == 0))
|
2016-05-01 09:26:56 -06:00
|
|
|
{
|
2016-05-01 08:41:30 -06:00
|
|
|
orientation = @"landscape";
|
2018-06-25 12:43:51 -06:00
|
|
|
} else {
|
2016-05-01 08:41:30 -06:00
|
|
|
orientation = @"portrait";
|
2018-06-25 12:43:51 -06:00
|
|
|
}
|
2016-04-08 03:10:22 -06:00
|
|
|
}
|
2018-06-25 12:43:51 -06:00
|
|
|
|
|
|
|
if (self.onVideoLoad && _videoLoadStarted) {
|
2017-01-16 09:27:08 -07:00
|
|
|
self.onVideoLoad(@{@"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],
|
|
|
|
@"naturalSize": @{
|
2018-06-25 12:43:51 -06:00
|
|
|
@"width": width,
|
|
|
|
@"height": height,
|
|
|
|
@"orientation": orientation
|
|
|
|
},
|
2018-06-11 21:55:23 -06:00
|
|
|
@"textTracks": [self getTextTrackInfo],
|
2017-01-16 09:27:08 -07:00
|
|
|
@"target": self.reactTag});
|
2018-06-25 12:43:51 -06:00
|
|
|
}
|
|
|
|
_videoLoadStarted = NO;
|
|
|
|
|
2015-08-05 06:52:30 -06:00
|
|
|
[self attachListeners];
|
|
|
|
[self applyModifiers];
|
2018-06-25 12:43:51 -06:00
|
|
|
} else if (_playerItem.status == AVPlayerItemStatusFailed && self.onVideoError) {
|
2016-12-12 17:16:11 -07:00
|
|
|
self.onVideoError(@{@"error": @{@"code": [NSNumber numberWithInteger: _playerItem.error.code],
|
|
|
|
@"domain": _playerItem.error.domain},
|
2018-06-25 12:43:51 -06:00
|
|
|
@"target": self.reactTag});
|
2015-08-05 06:52:30 -06:00
|
|
|
}
|
2016-02-18 17:45:09 -07:00
|
|
|
} else if ([keyPath isEqualToString:playbackBufferEmptyKeyPath]) {
|
|
|
|
_playerBufferEmpty = YES;
|
2017-01-11 05:51:45 -07:00
|
|
|
self.onVideoBuffer(@{@"isBuffering": @(YES), @"target": self.reactTag});
|
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-09-19 18:49:12 -06:00
|
|
|
if ((!(_controls || _fullscreenPlayerPresented) || _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;
|
2017-01-11 05:51:45 -07:00
|
|
|
self.onVideoBuffer(@{@"isBuffering": @(NO), @"target": self.reactTag});
|
2015-04-08 00:49:14 -06:00
|
|
|
}
|
2018-06-25 12:43:51 -06:00
|
|
|
} else if (object == _playerLayer) {
|
|
|
|
if([keyPath isEqualToString:readyForDisplayKeyPath] && [change objectForKey:NSKeyValueChangeNewKey]) {
|
|
|
|
if([change objectForKey:NSKeyValueChangeNewKey] && self.onReadyForDisplay) {
|
|
|
|
self.onReadyForDisplay(@{@"target": self.reactTag});
|
|
|
|
}
|
2016-04-28 06:28:29 -06:00
|
|
|
}
|
2016-04-28 06:37:45 -06:00
|
|
|
} else if (object == _player) {
|
2018-06-25 12:43:51 -06:00
|
|
|
if([keyPath isEqualToString:playbackRate]) {
|
|
|
|
if(self.onPlaybackRateChange) {
|
|
|
|
self.onPlaybackRateChange(@{@"playbackRate": [NSNumber numberWithFloat:_player.rate],
|
|
|
|
@"target": self.reactTag});
|
2016-04-28 06:37:45 -06:00
|
|
|
}
|
2018-06-25 12:43:51 -06:00
|
|
|
if(_playbackStalled && _player.rate > 0) {
|
|
|
|
if(self.onPlaybackResume) {
|
|
|
|
self.onPlaybackResume(@{@"playbackRate": [NSNumber numberWithFloat:_player.rate],
|
|
|
|
@"target": self.reactTag});
|
|
|
|
}
|
|
|
|
_playbackStalled = NO;
|
|
|
|
}
|
|
|
|
}
|
2015-04-08 00:49:14 -06:00
|
|
|
} else {
|
2018-06-25 12:43:51 -06: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
|
2017-10-24 01:47:43 -06:00
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self
|
|
|
|
name:AVPlayerItemDidPlayToEndTimeNotification
|
|
|
|
object:[_player currentItem]];
|
2015-07-18 06:38:56 -06:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(playerItemDidReachEnd:)
|
|
|
|
name:AVPlayerItemDidPlayToEndTimeNotification
|
|
|
|
object:[_player currentItem]];
|
2017-10-24 01:47:43 -06:00
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self
|
|
|
|
name:AVPlayerItemPlaybackStalledNotification
|
|
|
|
object:nil];
|
2016-04-28 06:38:21 -06:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(playbackStalled:)
|
|
|
|
name:AVPlayerItemPlaybackStalledNotification
|
|
|
|
object:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)playbackStalled:(NSNotification *)notification
|
|
|
|
{
|
2017-01-16 09:27:08 -07:00
|
|
|
if(self.onPlaybackStalled) {
|
|
|
|
self.onPlaybackStalled(@{@"target": self.reactTag});
|
|
|
|
}
|
2016-04-28 06:38:21 -06:00
|
|
|
_playbackStalled = YES;
|
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
|
|
|
{
|
2017-01-16 09:27:08 -07:00
|
|
|
if(self.onVideoEnd) {
|
|
|
|
self.onVideoEnd(@{@"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];
|
2018-05-15 23:19:12 -06:00
|
|
|
} else {
|
|
|
|
[self removePlayerTimeObserver];
|
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-10-30 03:34:54 -06:00
|
|
|
- (void)setPlayInBackground:(BOOL)playInBackground
|
|
|
|
{
|
2016-06-02 00:33:18 -06:00
|
|
|
_playInBackground = playInBackground;
|
2015-10-30 03:34:54 -06:00
|
|
|
}
|
|
|
|
|
2018-06-05 19:40:12 -06:00
|
|
|
- (void)setAllowsExternalPlayback:(BOOL)allowsExternalPlayback
|
|
|
|
{
|
|
|
|
_allowsExternalPlayback = allowsExternalPlayback;
|
|
|
|
_player.allowsExternalPlayback = _allowsExternalPlayback;
|
|
|
|
}
|
|
|
|
|
2016-04-29 05:55:34 -06:00
|
|
|
- (void)setPlayWhenInactive:(BOOL)playWhenInactive
|
|
|
|
{
|
2016-06-02 00:33:18 -06:00
|
|
|
_playWhenInactive = playWhenInactive;
|
2016-04-29 05:55:34 -06:00
|
|
|
}
|
|
|
|
|
2017-04-20 12:10:06 -06:00
|
|
|
- (void)setIgnoreSilentSwitch:(NSString *)ignoreSilentSwitch
|
|
|
|
{
|
|
|
|
_ignoreSilentSwitch = ignoreSilentSwitch;
|
|
|
|
[self applyModifiers];
|
|
|
|
}
|
|
|
|
|
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 {
|
2017-04-20 12:10:06 -06:00
|
|
|
if([_ignoreSilentSwitch isEqualToString:@"ignore"]) {
|
|
|
|
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
|
|
|
|
} else if([_ignoreSilentSwitch isEqualToString:@"obey"]) {
|
|
|
|
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
|
|
|
|
}
|
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
|
|
|
}
|
2016-08-04 06:32:04 -06: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
|
|
|
|
{
|
2018-06-20 23:09:45 -06:00
|
|
|
NSDictionary *info = @{
|
|
|
|
@"time": [NSNumber numberWithFloat:currentTime],
|
|
|
|
@"tolerance": [NSNumber numberWithInt:100]
|
|
|
|
};
|
|
|
|
[self setSeek:info];
|
2015-12-22 16:39:04 -07:00
|
|
|
}
|
|
|
|
|
2018-06-20 23:09:45 -06:00
|
|
|
- (void)setSeek:(NSDictionary *)info
|
2015-05-30 13:50:45 -06:00
|
|
|
{
|
2018-06-20 23:09:45 -06:00
|
|
|
NSNumber *seekTime = info[@"time"];
|
|
|
|
NSNumber *seekTolerance = info[@"tolerance"];
|
|
|
|
|
|
|
|
int timeScale = 1000;
|
2015-04-08 16:15:57 -06:00
|
|
|
|
|
|
|
AVPlayerItem *item = _player.currentItem;
|
|
|
|
if (item && item.status == AVPlayerItemStatusReadyToPlay) {
|
|
|
|
// TODO check loadedTimeRanges
|
|
|
|
|
2018-06-20 23:09:45 -06:00
|
|
|
CMTime cmSeekTime = CMTimeMakeWithSeconds([seekTime floatValue], timeScale);
|
2015-04-08 16:15:57 -06:00
|
|
|
CMTime current = item.currentTime;
|
|
|
|
// TODO figure out a good tolerance level
|
2018-06-20 23:09:45 -06:00
|
|
|
CMTime tolerance = CMTimeMake([seekTolerance floatValue], timeScale);
|
2017-03-21 14:25:58 -06:00
|
|
|
BOOL wasPaused = _paused;
|
2016-08-04 06:32:04 -06:00
|
|
|
|
2015-04-08 16:15:57 -06:00
|
|
|
if (CMTimeCompare(current, cmSeekTime) != 0) {
|
2017-03-21 14:25:58 -06:00
|
|
|
if (!wasPaused) [_player pause];
|
2015-07-18 06:38:56 -06:00
|
|
|
[_player seekToTime:cmSeekTime toleranceBefore:tolerance toleranceAfter:tolerance completionHandler:^(BOOL finished) {
|
2018-05-15 23:19:12 -06:00
|
|
|
if (!_timeObserver) {
|
|
|
|
[self addPlayerTimeObserver];
|
|
|
|
}
|
2017-09-07 06:16:44 -06:00
|
|
|
if (!wasPaused) {
|
2018-06-20 23:09:45 -06:00
|
|
|
[self setPaused:false];
|
2017-09-07 06:16:44 -06:00
|
|
|
}
|
2017-01-16 09:27:08 -07:00
|
|
|
if(self.onVideoSeek) {
|
|
|
|
self.onVideoSeek(@{@"currentTime": [NSNumber numberWithFloat:CMTimeGetSeconds(item.currentTime)],
|
2018-06-20 23:09:45 -06:00
|
|
|
@"seekTime": seekTime,
|
2017-01-16 09:27:08 -07:00
|
|
|
@"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;
|
2018-06-20 23:09:45 -06:00
|
|
|
_pendingSeekTime = [seekTime floatValue];
|
2015-04-08 16:15:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-06-02 03:24:13 -06:00
|
|
|
[self setSelectedTextTrack:_selectedTextTrack];
|
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];
|
2018-06-05 19:40:12 -06:00
|
|
|
[self setAllowsExternalPlayback:_allowsExternalPlayback];
|
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
|
|
|
}
|
|
|
|
|
2018-06-02 03:24:13 -06:00
|
|
|
- (void)setSelectedTextTrack:(NSDictionary *)selectedTextTrack {
|
|
|
|
_selectedTextTrack = selectedTextTrack;
|
|
|
|
NSString *type = selectedTextTrack[@"type"];
|
|
|
|
AVMediaSelectionGroup *group = [_player.currentItem.asset
|
|
|
|
mediaSelectionGroupForMediaCharacteristic:AVMediaCharacteristicLegible];
|
|
|
|
AVMediaSelectionOption *option;
|
2018-06-02 20:41:25 -06:00
|
|
|
|
2018-06-02 03:24:13 -06:00
|
|
|
if ([type isEqualToString:@"disabled"]) {
|
|
|
|
// Do nothing. We want to ensure option is nil
|
|
|
|
} else if ([type isEqualToString:@"language"] || [type isEqualToString:@"title"]) {
|
|
|
|
NSString *value = selectedTextTrack[@"value"];
|
|
|
|
for (int i = 0; i < group.options.count; ++i) {
|
|
|
|
AVMediaSelectionOption *currentOption = [group.options objectAtIndex:i];
|
|
|
|
NSString *optionValue;
|
|
|
|
if ([type isEqualToString:@"language"]) {
|
|
|
|
optionValue = [currentOption extendedLanguageTag];
|
|
|
|
} else {
|
|
|
|
optionValue = [[[currentOption commonMetadata]
|
|
|
|
valueForKey:@"value"]
|
|
|
|
objectAtIndex:0];
|
|
|
|
}
|
|
|
|
if ([value isEqualToString:optionValue]) {
|
|
|
|
option = currentOption;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-06-02 20:41:25 -06:00
|
|
|
//} else if ([type isEqualToString:@"default"]) {
|
|
|
|
// option = group.defaultOption; */
|
2018-06-02 03:24:13 -06:00
|
|
|
} else if ([type isEqualToString:@"index"]) {
|
|
|
|
if ([selectedTextTrack[@"value"] isKindOfClass:[NSNumber class]]) {
|
|
|
|
int index = [selectedTextTrack[@"value"] intValue];
|
|
|
|
if (group.options.count > index) {
|
|
|
|
option = [group.options objectAtIndex:index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { // default. invalid type or "system"
|
2018-06-02 20:41:25 -06:00
|
|
|
[_player.currentItem selectMediaOptionAutomaticallyInMediaSelectionGroup:group];
|
2018-06-02 03:24:13 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a match isn't found, option will be nil and text tracks will be disabled
|
|
|
|
[_player.currentItem selectMediaOption:option inMediaSelectionGroup:group];
|
|
|
|
}
|
|
|
|
|
2018-06-11 21:55:23 -06:00
|
|
|
- (NSArray *)getTextTrackInfo
|
|
|
|
{
|
|
|
|
NSMutableArray *textTracks = [[NSMutableArray alloc] init];
|
|
|
|
AVMediaSelectionGroup *group = [_player.currentItem.asset
|
|
|
|
mediaSelectionGroupForMediaCharacteristic:AVMediaCharacteristicLegible];
|
|
|
|
for (int i = 0; i < group.options.count; ++i) {
|
|
|
|
AVMediaSelectionOption *currentOption = [group.options objectAtIndex:i];
|
2018-06-20 16:26:24 -06:00
|
|
|
NSString *title = @"";
|
|
|
|
NSArray *values = [[currentOption commonMetadata] valueForKey:@"value"];
|
|
|
|
if (values.count > 0) {
|
|
|
|
title = [values objectAtIndex:0];
|
|
|
|
}
|
|
|
|
NSString *language = [currentOption extendedLanguageTag] ? [currentOption extendedLanguageTag] : @"";
|
2018-06-11 21:55:23 -06:00
|
|
|
NSDictionary *textTrack = @{
|
|
|
|
@"index": [NSNumber numberWithInt:i],
|
|
|
|
@"title": title,
|
2018-06-20 16:26:24 -06:00
|
|
|
@"language": language
|
2018-06-11 21:55:23 -06:00
|
|
|
};
|
|
|
|
[textTracks addObject:textTrack];
|
|
|
|
}
|
|
|
|
return textTracks;
|
|
|
|
}
|
|
|
|
|
2016-03-31 12:34:22 -06:00
|
|
|
- (BOOL)getFullscreen
|
|
|
|
{
|
2016-04-01 06:55:23 -06:00
|
|
|
return _fullscreenPlayerPresented;
|
2016-03-31 12:34:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setFullscreen:(BOOL)fullscreen
|
|
|
|
{
|
2016-04-01 06:56:03 -06:00
|
|
|
if( fullscreen && !_fullscreenPlayerPresented )
|
2016-03-31 12:34:22 -06:00
|
|
|
{
|
|
|
|
// Ensure player view controller is not null
|
|
|
|
if( !_playerViewController )
|
|
|
|
{
|
|
|
|
[self usePlayerViewController];
|
|
|
|
}
|
|
|
|
// Set presentation style to fullscreen
|
2016-04-01 04:34:52 -06:00
|
|
|
[_playerViewController setModalPresentationStyle:UIModalPresentationFullScreen];
|
2016-08-04 06:32:04 -06:00
|
|
|
|
2016-03-31 12:34:22 -06:00
|
|
|
// 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;
|
2017-01-16 09:27:08 -07:00
|
|
|
if(self.onVideoFullscreenPlayerWillPresent) {
|
|
|
|
self.onVideoFullscreenPlayerWillPresent(@{@"target": self.reactTag});
|
|
|
|
}
|
2016-03-31 12:34:22 -06:00
|
|
|
[viewController presentViewController:_playerViewController animated:true completion:^{
|
|
|
|
_playerViewController.showsPlaybackControls = YES;
|
2016-04-01 06:55:23 -06:00
|
|
|
_fullscreenPlayerPresented = fullscreen;
|
2017-01-16 09:27:08 -07:00
|
|
|
if(self.onVideoFullscreenPlayerDidPresent) {
|
|
|
|
self.onVideoFullscreenPlayerDidPresent(@{@"target": self.reactTag});
|
|
|
|
}
|
2016-03-31 12:34:22 -06:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
2016-04-01 06:56:03 -06:00
|
|
|
else if ( !fullscreen && _fullscreenPlayerPresented )
|
2016-03-31 12:34:22 -06:00
|
|
|
{
|
2016-04-01 02:52:05 -06:00
|
|
|
[self videoPlayerViewControllerWillDismiss:_playerViewController];
|
2016-03-31 12:34:22 -06:00
|
|
|
[_presentingViewController dismissViewControllerAnimated:true completion:^{
|
2016-04-01 02:52:05 -06:00
|
|
|
[self videoPlayerViewControllerDidDismiss:_playerViewController];
|
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];
|
2016-08-29 13:48:36 -06:00
|
|
|
// to prevent video from being animated when resizeMode is 'cover'
|
|
|
|
// resize mode must be set before subview is added
|
|
|
|
[self setResizeMode:_resizeMode];
|
2015-12-22 16:39:04 -07:00
|
|
|
[self addSubview:_playerViewController.view];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)usePlayerLayer
|
|
|
|
{
|
|
|
|
if( _player )
|
|
|
|
{
|
|
|
|
_playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
|
|
|
|
_playerLayer.frame = self.bounds;
|
|
|
|
_playerLayer.needsDisplayOnBoundsChange = YES;
|
2016-08-04 06:32:04 -06:00
|
|
|
|
2016-08-29 13:48:36 -06:00
|
|
|
// to prevent video from being animated when resizeMode is 'cover'
|
|
|
|
// resize mode must be set before layer is added
|
|
|
|
[self setResizeMode:_resizeMode];
|
2016-04-28 06:25:45 -06:00
|
|
|
[_playerLayer addObserver:self forKeyPath:readyForDisplayKeyPath options:NSKeyValueObservingOptionNew context:nil];
|
2018-06-20 17:33:50 -06:00
|
|
|
_playerLayerObserverSet = YES;
|
2016-08-04 06:32:04 -06:00
|
|
|
|
2015-12-22 16:39:04 -07:00
|
|
|
[self.layer addSublayer:_playerLayer];
|
|
|
|
self.layer.needsDisplayOnBoundsChange = YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setControls:(BOOL)controls
|
|
|
|
{
|
|
|
|
if( _controls != controls || (!_playerLayer && !_playerViewController) )
|
|
|
|
{
|
|
|
|
_controls = controls;
|
|
|
|
if( _controls )
|
|
|
|
{
|
2016-04-28 06:25:45 -06:00
|
|
|
[self removePlayerLayer];
|
2015-12-22 16:39:04 -07:00
|
|
|
[self usePlayerViewController];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[_playerViewController.view removeFromSuperview];
|
|
|
|
_playerViewController = nil;
|
|
|
|
[self usePlayerLayer];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-01 12:23:50 -06:00
|
|
|
- (void)setProgressUpdateInterval:(float)progressUpdateInterval
|
|
|
|
{
|
|
|
|
_progressUpdateInterval = progressUpdateInterval;
|
2017-09-28 19:37:52 -06:00
|
|
|
|
|
|
|
if (_timeObserver) {
|
|
|
|
[self removePlayerTimeObserver];
|
|
|
|
[self addPlayerTimeObserver];
|
|
|
|
}
|
2016-10-01 12:23:50 -06:00
|
|
|
}
|
|
|
|
|
2016-04-28 06:25:45 -06:00
|
|
|
- (void)removePlayerLayer
|
|
|
|
{
|
|
|
|
[_playerLayer removeFromSuperlayer];
|
2018-06-20 17:33:50 -06:00
|
|
|
if (_playerLayerObserverSet) {
|
|
|
|
[_playerLayer removeObserver:self forKeyPath:readyForDisplayKeyPath];
|
|
|
|
_playerLayerObserverSet = NO;
|
|
|
|
}
|
2016-04-28 06:25:45 -06:00
|
|
|
_playerLayer = nil;
|
|
|
|
}
|
|
|
|
|
2016-04-01 02:52:05 -06:00
|
|
|
#pragma mark - RCTVideoPlayerViewControllerDelegate
|
|
|
|
|
|
|
|
- (void)videoPlayerViewControllerWillDismiss:(AVPlayerViewController *)playerViewController
|
|
|
|
{
|
2017-01-16 09:27:08 -07:00
|
|
|
if (_playerViewController == playerViewController && _fullscreenPlayerPresented && self.onVideoFullscreenPlayerWillDismiss)
|
2016-04-01 02:52:05 -06:00
|
|
|
{
|
2016-12-12 17:16:11 -07:00
|
|
|
self.onVideoFullscreenPlayerWillDismiss(@{@"target": self.reactTag});
|
2016-04-01 02:52:05 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)videoPlayerViewControllerDidDismiss:(AVPlayerViewController *)playerViewController
|
|
|
|
{
|
2016-04-01 06:55:23 -06:00
|
|
|
if (_playerViewController == playerViewController && _fullscreenPlayerPresented)
|
2016-04-01 02:52:05 -06:00
|
|
|
{
|
2016-04-01 06:55:23 -06:00
|
|
|
_fullscreenPlayerPresented = false;
|
2016-04-01 02:52:05 -06:00
|
|
|
_presentingViewController = nil;
|
2017-03-15 05:21:12 -06:00
|
|
|
_playerViewController = nil;
|
2016-04-01 04:52:23 -06:00
|
|
|
[self applyModifiers];
|
2017-01-16 09:27:08 -07:00
|
|
|
if(self.onVideoFullscreenPlayerDidDismiss) {
|
|
|
|
self.onVideoFullscreenPlayerDidDismiss(@{@"target": self.reactTag});
|
|
|
|
}
|
2016-04-01 02:52:05 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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];
|
|
|
|
}
|
2016-08-04 06:32:04 -06:00
|
|
|
|
2015-12-22 16:39:04 -07:00
|
|
|
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;
|
2016-08-04 06:32:04 -06:00
|
|
|
|
2015-12-22 16:39:04 -07:00
|
|
|
// 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];
|
2016-05-17 01:40:58 -06:00
|
|
|
if (_playbackRateObserverRegistered) {
|
2016-05-19 12:27:23 -06:00
|
|
|
[_player removeObserver:self forKeyPath:playbackRate context:nil];
|
2016-05-17 01:38:35 -06:00
|
|
|
_playbackRateObserverRegistered = NO;
|
2016-05-17 01:40:58 -06:00
|
|
|
}
|
2015-04-10 01:15:19 -06:00
|
|
|
_player = nil;
|
|
|
|
|
2016-04-28 06:25:45 -06:00
|
|
|
[self removePlayerLayer];
|
2016-08-04 06:32:04 -06:00
|
|
|
|
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
|