Align more closely with react native style

This commit is contained in:
Brent Vatne 2015-05-30 12:50:45 -07:00
parent c5eb6b4fec
commit a20b94757d

View File

@ -41,7 +41,8 @@ static NSString *const statusKeyPath = @"status";
BOOL _paused; BOOL _paused;
} }
- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher { - (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher
{
if ((self = [super init])) { if ((self = [super init])) {
_eventDispatcher = eventDispatcher; _eventDispatcher = eventDispatcher;
_rate = 1.0; _rate = 1.0;
@ -57,7 +58,8 @@ static NSString *const statusKeyPath = @"status";
#pragma mark - Progress #pragma mark - Progress
- (void)sendProgressUpdate { - (void)sendProgressUpdate
{
AVPlayerItem *video = [_player currentItem]; AVPlayerItem *video = [_player currentItem];
if (video == nil || video.status != AVPlayerItemStatusReadyToPlay) { if (video == nil || video.status != AVPlayerItemStatusReadyToPlay) {
return; return;
@ -74,11 +76,13 @@ static NSString *const statusKeyPath = @"status";
} }
} }
- (void)stopProgressTimer { - (void)stopProgressTimer
{
[_progressUpdateTimer invalidate]; [_progressUpdateTimer invalidate];
} }
- (void)startProgressTimer { - (void)startProgressTimer
{
_progressUpdateInterval = 250; _progressUpdateInterval = 250;
_prevProgressUpdateTime = nil; _prevProgressUpdateTime = nil;
@ -114,7 +118,8 @@ static NSString *const statusKeyPath = @"status";
#pragma mark - Player and source #pragma mark - Player and source
- (void)setSrc:(NSDictionary *)source { - (void)setSrc:(NSDictionary *)source
{
[self removePlayerItemObserver]; [self removePlayerItemObserver];
_playerItem = [self playerItemForSource:source]; _playerItem = [self playerItemForSource:source];
[self addPlayerItemObserver]; [self addPlayerItemObserver];
@ -142,7 +147,8 @@ static NSString *const statusKeyPath = @"status";
}]; }];
} }
- (AVPlayerItem*)playerItemForSource:(NSDictionary *)source { - (AVPlayerItem*)playerItemForSource:(NSDictionary *)source
{
bool isNetwork = [RCTConvert BOOL:[source objectForKey:@"isNetwork"]]; bool isNetwork = [RCTConvert BOOL:[source objectForKey:@"isNetwork"]];
bool isAsset = [RCTConvert BOOL:[source objectForKey:@"isAsset"]]; bool isAsset = [RCTConvert BOOL:[source objectForKey:@"isAsset"]];
NSString *uri = [source objectForKey:@"uri"]; NSString *uri = [source objectForKey:@"uri"];
@ -160,7 +166,8 @@ static NSString *const statusKeyPath = @"status";
return [AVPlayerItem playerItemWithURL:url]; return [AVPlayerItem playerItemWithURL:url];
} }
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (object == _playerItem) { if (object == _playerItem) {
if (_playerItem.status == AVPlayerItemStatusReadyToPlay) { if (_playerItem.status == AVPlayerItemStatusReadyToPlay) {
float duration = CMTimeGetSeconds(_playerItem.asset.duration); float duration = CMTimeGetSeconds(_playerItem.asset.duration);
@ -198,7 +205,8 @@ static NSString *const statusKeyPath = @"status";
} }
} }
- (void)attachListeners { - (void)attachListeners
{
// listen for end of file // listen for end of file
[[NSNotificationCenter defaultCenter] addObserver:self [[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(notifyEnd:) selector:@selector(notifyEnd:)
@ -207,7 +215,8 @@ static NSString *const statusKeyPath = @"status";
} }
- (void)playerItemDidReachEnd:(NSNotification *)notification { - (void)playerItemDidReachEnd:(NSNotification *)notification
{
AVPlayerItem *item = [notification object]; AVPlayerItem *item = [notification object];
[item seekToTime:kCMTimeZero]; [item seekToTime:kCMTimeZero];
[self applyModifiers]; [self applyModifiers];
@ -215,11 +224,13 @@ static NSString *const statusKeyPath = @"status";
#pragma mark - Prop setters #pragma mark - Prop setters
- (void)setResizeMode:(NSString*)mode { - (void)setResizeMode:(NSString*)mode
{
_playerLayer.videoGravity = mode; _playerLayer.videoGravity = mode;
} }
- (void)setPaused:(BOOL)paused { - (void)setPaused:(BOOL)paused
{
if (paused) { if (paused) {
[self stopProgressTimer]; [self stopProgressTimer];
[_player pause]; [_player pause];
@ -231,7 +242,8 @@ static NSString *const statusKeyPath = @"status";
_paused = paused; _paused = paused;
} }
- (void)setSeek:(float)seekTime { - (void)setSeek:(float)seekTime
{
int timeScale = 10000; int timeScale = 10000;
AVPlayerItem *item = _player.currentItem; AVPlayerItem *item = _player.currentItem;
@ -263,22 +275,26 @@ static NSString *const statusKeyPath = @"status";
} }
} }
- (void)setRate:(float)rate { - (void)setRate:(float)rate
{
_rate = rate; _rate = rate;
[self applyModifiers]; [self applyModifiers];
} }
- (void)setMuted:(BOOL)muted { - (void)setMuted:(BOOL)muted
{
_muted = muted; _muted = muted;
[self applyModifiers]; [self applyModifiers];
} }
- (void)setVolume:(float)volume { - (void)setVolume:(float)volume
{
_volume = volume; _volume = volume;
[self applyModifiers]; [self applyModifiers];
} }
- (void)applyModifiers { - (void)applyModifiers
{
/* volume must be set to 0 if muted is YES, or the video freezes playback */ /* volume must be set to 0 if muted is YES, or the video freezes playback */
if (_muted) { if (_muted) {
[_player setVolume:0]; [_player setVolume:0];
@ -292,14 +308,16 @@ static NSString *const statusKeyPath = @"status";
[self setPaused:_paused]; [self setPaused:_paused];
} }
- (void)setRepeatEnabled { - (void)setRepeatEnabled
{
[[NSNotificationCenter defaultCenter] addObserver:self [[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:) selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification name:AVPlayerItemDidPlayToEndTimeNotification
object:[_player currentItem]]; object:[_player currentItem]];
} }
- (void) setRepeatDisabled { - (void)setRepeatDisabled
{
[[NSNotificationCenter defaultCenter] removeObserver:self]; [[NSNotificationCenter defaultCenter] removeObserver:self];
} }
@ -313,24 +331,28 @@ static NSString *const statusKeyPath = @"status";
#pragma mark - React View Management #pragma mark - React View Management
- (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");
return; return;
} }
- (void)removeReactSubview:(UIView *)subview { - (void)removeReactSubview:(UIView *)subview
{
RCTLogError(@"video cannot have any subviews"); RCTLogError(@"video cannot have any subviews");
return; return;
} }
- (void)layoutSubviews { - (void)layoutSubviews
{
[super layoutSubviews]; [super layoutSubviews];
_playerLayer.frame = self.bounds; _playerLayer.frame = self.bounds;
} }
#pragma mark - Lifecycle #pragma mark - Lifecycle
- (void)removeFromSuperview { - (void)removeFromSuperview
{
[_progressUpdateTimer invalidate]; [_progressUpdateTimer invalidate];
_prevProgressUpdateTime = nil; _prevProgressUpdateTime = nil;