Commit 4666b407 authored by Brent Vatne's avatar Brent Vatne
Browse files

Add an onLoad callback to fetch video details

parent 432d1561
Loading
Loading
Loading
Loading
+5 −1
Changes for RCTVideo.h: 5 added lines, 1 removed line.
Original line number Diff line number Diff line
#import <UIKit/UIKit.h>
#import "RCTView.h"

@class RCTEventDispatcher;

@interface RCTVideo : UIView

- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher NS_DESIGNATED_INITIALIZER;

@end
+24 −5
Changes for RCTVideo.m: 24 added lines, 5 removed lines.
Original line number Diff line number Diff line
#import "RCTVideo.h"
#import "RCTLog.h"
#import "RCTBridgeModule.h"
#import "RCTEventDispatcher.h"
#import "UIView+React.h"
#import <AVFoundation/AVFoundation.h>

@implementation RCTVideo
{
    AVPlayer *_player;
    AVPlayerLayer *_playerLayer;
    NSURL *_videoURL;
    RCTEventDispatcher *_eventDispatcher;
}

- (id)init
- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher
{
  if ((self = [super init])) {

    _eventDispatcher = eventDispatcher;
  }
  return self;
}

- (void)setSrc:(NSString *)source
{
  NSURL *videoURL = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:source ofType:@"mp4"]];
  _player = [AVPlayer playerWithURL:videoURL];
  _videoURL = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:source ofType:@"mp4"]];
  _player = [AVPlayer playerWithURL:_videoURL];
  _player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
  _playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
  _playerLayer.frame = self.bounds;
  _playerLayer.needsDisplayOnBoundsChange = YES;

  [self.layer addSublayer:_playerLayer];
  self.layer.needsDisplayOnBoundsChange = YES;
  [_player play];

  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
  }];
}

- (void)setResizeMode:(NSString*)mode
+12 −1
Changes for RCTVideoManager.m: 12 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -9,7 +9,18 @@

- (UIView *)view
{
  return [[RCTVideo alloc] init];
  return [[RCTVideo alloc] initWithEventDispatcher:self.bridge.eventDispatcher];
}

/* onLoadStart, onLoad, and onError to stay consistent with Image */

- (NSDictionary *)customDirectEventTypes
{
  return @{
    @"videoLoaded": @{
      @"registrationName": @"onLoad"
    },
  };
}

RCT_EXPORT_VIEW_PROPERTY(src, NSString);
+8 −2
Changes for Video.ios.js: 8 added lines, 2 removed lines.
Original line number Diff line number Diff line
var React = require('React');
var React = require('react-native');
var NativeModules = require('NativeModules');
var ReactIOSViewAttributes = require('ReactIOSViewAttributes');
var StyleSheet = require('StyleSheet');
@@ -18,6 +18,7 @@ var Video = React.createClass({
    resizeMode: PropTypes.string,
    repeat: PropTypes.bool,
    pause: PropTypes.bool,
    onLoad: PropTypes.func,
  },

  mixins: [NativeMethodsMixin],
@@ -27,7 +28,11 @@ var Video = React.createClass({
    validAttributes: ReactIOSViewAttributes.UIView
  },

  render: function() {
  _onLoad(event) {
    this.props.onLoad && this.props.onLoad(event.nativeEvent);
  },

  render() {
    var style = flattenStyle([styles.base, this.props.style]);
    var source = this.props.source;

@@ -46,6 +51,7 @@ var Video = React.createClass({
      style,
      resizeMode: resizeMode,
      src: source,
      onLoad: this._onLoad,
    });

    return <RCTVideo {... nativeProps} />
+1 −1
Changes for package.json: 1 added line, 1 removed line.
Original line number Diff line number Diff line
{
  "name": "react-native-video",
  "version": "0.1.7",
  "version": "0.1.8",
  "description": "A <Video> element for react-native",
  "main": "Video.ios.js",
  "author": "Brent Vatne <brentvatne@gmail.com> (https://github.com/brentvatne)",