From 4666b407e01f2c70a12bb90ea1cf4805c58de55c Mon Sep 17 00:00:00 2001 From: Brent Vatne Date: Mon, 6 Apr 2015 12:17:32 -0700 Subject: [PATCH] Add an onLoad callback to fetch video details --- RCTVideo.h | 6 +++++- RCTVideo.m | 39 +++++++++++++++++++++++++++++---------- RCTVideoManager.m | 15 +++++++++++++-- Video.ios.js | 12 +++++++++--- package.json | 2 +- 5 files changed, 57 insertions(+), 17 deletions(-) diff --git a/RCTVideo.h b/RCTVideo.h index f0ba7ec3..1fc82e43 100644 --- a/RCTVideo.h +++ b/RCTVideo.h @@ -1,5 +1,9 @@ -#import +#import "RCTView.h" + +@class RCTEventDispatcher; @interface RCTVideo : UIView +- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher NS_DESIGNATED_INITIALIZER; + @end diff --git a/RCTVideo.m b/RCTVideo.m index 12024132..b68130ff 100644 --- a/RCTVideo.m +++ b/RCTVideo.m @@ -1,32 +1,51 @@ #import "RCTVideo.h" #import "RCTLog.h" +#import "RCTBridgeModule.h" +#import "RCTEventDispatcher.h" +#import "UIView+React.h" #import @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 @@ -36,11 +55,11 @@ - (void)setPause:(BOOL)wantsToPause { - if (wantsToPause) { - [_player pause]; - } else { - [_player play]; - } + if (wantsToPause) { + [_player pause]; + } else { + [_player play]; + } } diff --git a/RCTVideoManager.m b/RCTVideoManager.m index ba67946c..5514a747 100644 --- a/RCTVideoManager.m +++ b/RCTVideoManager.m @@ -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); @@ -25,4 +36,4 @@ RCT_EXPORT_VIEW_PROPERTY(pause, BOOL); @"ScaleAspectFill": AVLayerVideoGravityResizeAspectFill}; } -@end \ No newline at end of file +@end diff --git a/Video.ios.js b/Video.ios.js index 0a64dd8b..18ca81fa 100644 --- a/Video.ios.js +++ b/Video.ios.js @@ -1,4 +1,4 @@ -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 @@ -54,7 +60,7 @@ var Video = React.createClass({ var RCTVideo = createReactIOSNativeComponentClass({ validAttributes: merge(ReactIOSViewAttributes.UIView, - {src: true, resizeMode: true, repeat: true, pause: true}), + {src: true, resizeMode: true, repeat: true, pause: true}), uiViewClassName: 'RCTVideo', }); diff --git a/package.json b/package.json index 7c06cdfc..c8c84311 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-video", - "version": "0.1.7", + "version": "0.1.8", "description": "A