Commit bd1dc1f0 authored by Brent Vatne's avatar Brent Vatne
Browse files

Fix local playback with source dictionary, set default rate/volume

parent e9e48bb6
Loading
Loading
Loading
Loading
+25 −31
Changes for RCTVideo.m: 25 added lines, 31 removed lines.
Original line number Diff line number Diff line
@@ -36,6 +36,8 @@ static NSString *const statusKeyPath = @"status";
- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher {
  if ((self = [super init])) {
    _eventDispatcher = eventDispatcher;
    _rate = 1.0;
    _volume = 1.0;
  }
  return self;
}
@@ -64,7 +66,6 @@ static NSString *const statusKeyPath = @"status";
}

- (void)startProgressTimer {
  /* Initialize videoProgress status publisher */
  _progressUpdateInterval = 250;
  _prevProgressUpdateTime = nil;

@@ -76,24 +77,6 @@ static NSString *const statusKeyPath = @"status";

#pragma mark - Player and source

- (AVPlayerItem*)playerItemForSource:(NSDictionary *)source {
  bool isNetwork = [source objectForKey:@"isNetwork"];
  bool isAsset = [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];
}

- (void)setSrc:(NSDictionary *)source {
  [_playerItem removeObserver:self forKeyPath:statusKeyPath];
  _playerItem = [self playerItemForSource:source];
@@ -122,6 +105,24 @@ static NSString *const statusKeyPath = @"status";
  }];
}

- (AVPlayerItem*)playerItemForSource:(NSDictionary *)source {
  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];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
  if (object == _playerItem) {
    if (_playerItem.status == AVPlayerItemStatusReadyToPlay) {
@@ -158,9 +159,10 @@ static NSString *const statusKeyPath = @"status";
    AVPlayerItem *item = [notification object];
    [item seekToTime:kCMTimeZero];
    [_player play];
    [self applyModifiers];
}

#pragma mark - additional prop setters
#pragma mark - Prop setters

- (void)setResizeMode:(NSString*)mode {
  _playerLayer.videoGravity = mode;
@@ -197,8 +199,7 @@ static NSString *const statusKeyPath = @"status";

- (void)applyModifiers
{
  /* volume must be set to 0 if muted is YES, or the video seems to
   * freeze */
  /* volume must be set to 0 if muted is YES, or the video freezes playback */
  if (_muted) {
    [_player setVolume:0];
    [_player setMuted:YES];
@@ -210,13 +211,6 @@ static NSString *const statusKeyPath = @"status";
  [_player setRate:_rate];
}

- (void)playerItemDidReachEnd:(NSNotification *)notification {
  AVPlayerItem *item = [notification object];
  [item seekToTime:kCMTimeZero];
  [_player play];
  [self applyModifiers];
}

- (void)setRepeatEnabled {
  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(playerItemDidReachEnd:)
@@ -236,7 +230,7 @@ static NSString *const statusKeyPath = @"status";
  }
}

#pragma mark - react and view related
#pragma mark - React View Management

- (void)insertReactSubview:(UIView *)view atIndex:(NSInteger)atIndex {
  RCTLogError(@"video cannot have any subviews");
@@ -253,7 +247,7 @@ static NSString *const statusKeyPath = @"status";
  _playerLayer.frame = self.bounds;
}

#pragma mark - lifecycle
#pragma mark - Lifecycle

- (void)removeFromSuperview
{
+2 −2
Changes for Video.ios.js: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -90,8 +90,8 @@ var Video = React.createClass({

var RCTVideo = createReactIOSNativeComponentClass({
  validAttributes: merge(ReactIOSViewAttributes.UIView,
    {src: {diff: deepDiffer}, resizeMode: true, repeat: true, paused: true, muted: true,
     volume: true, rate: true}),
    {src: {diff: deepDiffer}, resizeMode: true, repeat: true,
     paused: true, muted: true, volume: true, rate: true}),
  uiViewClassName: 'RCTVideo',
});