2015-04-06 13:17:32 -06:00
|
|
|
var React = require('react-native');
|
2015-07-28 16:46:05 -06:00
|
|
|
var { StyleSheet, requireNativeComponent, PropTypes, NativeModules, } = React;
|
|
|
|
|
2015-03-30 23:07:55 -06:00
|
|
|
var VideoResizeMode = require('./VideoResizeMode');
|
2015-09-16 15:36:17 -06:00
|
|
|
var { extend } = require('lodash');
|
2015-07-28 16:46:05 -06:00
|
|
|
|
|
|
|
var VIDEO_REF = 'video';
|
2015-03-30 23:07:55 -06:00
|
|
|
|
|
|
|
var Video = React.createClass({
|
|
|
|
propTypes: {
|
2015-06-24 22:26:36 -06:00
|
|
|
/* Native only */
|
|
|
|
src: PropTypes.object,
|
|
|
|
seek: PropTypes.number,
|
|
|
|
|
|
|
|
/* Wrapper component */
|
2015-04-08 00:49:14 -06:00
|
|
|
source: PropTypes.object,
|
2015-04-05 11:17:03 -06:00
|
|
|
resizeMode: PropTypes.string,
|
2015-03-31 00:29:15 -06:00
|
|
|
repeat: PropTypes.bool,
|
2015-04-07 21:38:16 -06:00
|
|
|
paused: PropTypes.bool,
|
|
|
|
muted: PropTypes.bool,
|
|
|
|
volume: PropTypes.number,
|
|
|
|
rate: PropTypes.number,
|
2015-10-30 03:34:54 -06:00
|
|
|
playInBackground: PropTypes.bool,
|
2015-04-08 00:49:14 -06:00
|
|
|
onLoadStart: PropTypes.func,
|
2015-04-06 13:17:32 -06:00
|
|
|
onLoad: PropTypes.func,
|
2015-04-08 00:49:14 -06:00
|
|
|
onError: PropTypes.func,
|
2015-04-06 14:05:05 -06:00
|
|
|
onProgress: PropTypes.func,
|
2015-04-10 21:40:13 -06:00
|
|
|
onEnd: PropTypes.func,
|
2015-03-30 23:07:55 -06:00
|
|
|
},
|
|
|
|
|
2015-07-28 16:46:05 -06:00
|
|
|
setNativeProps(props) {
|
|
|
|
this.refs[VIDEO_REF].setNativeProps(props);
|
2015-03-30 23:07:55 -06:00
|
|
|
},
|
|
|
|
|
2015-04-08 00:49:14 -06:00
|
|
|
_onLoadStart(event) {
|
|
|
|
this.props.onLoadStart && this.props.onLoadStart(event.nativeEvent);
|
|
|
|
},
|
|
|
|
|
2015-04-06 13:17:32 -06:00
|
|
|
_onLoad(event) {
|
|
|
|
this.props.onLoad && this.props.onLoad(event.nativeEvent);
|
|
|
|
},
|
|
|
|
|
2015-04-08 00:49:14 -06:00
|
|
|
_onError(event) {
|
|
|
|
this.props.onError && this.props.onError(event.nativeEvent);
|
|
|
|
},
|
|
|
|
|
2015-04-06 14:05:05 -06:00
|
|
|
_onProgress(event) {
|
|
|
|
this.props.onProgress && this.props.onProgress(event.nativeEvent);
|
|
|
|
},
|
|
|
|
|
2015-04-08 16:15:57 -06:00
|
|
|
_onSeek(event) {
|
|
|
|
this.props.onSeek && this.props.onSeek(event.nativeEvent);
|
|
|
|
},
|
|
|
|
|
2015-04-09 07:50:20 -06:00
|
|
|
seek(time) {
|
2015-04-09 09:12:54 -06:00
|
|
|
this.setNativeProps({seek: parseFloat(time)});
|
2015-04-09 07:50:20 -06:00
|
|
|
},
|
|
|
|
|
2015-04-10 21:40:13 -06:00
|
|
|
_onEnd(event) {
|
|
|
|
this.props.onEnd && this.props.onEnd(event.nativeEvent);
|
|
|
|
},
|
|
|
|
|
2015-04-06 13:17:32 -06:00
|
|
|
render() {
|
2015-07-28 16:46:05 -06:00
|
|
|
var style = [styles.base, this.props.style];
|
2015-03-30 23:07:55 -06:00
|
|
|
var source = this.props.source;
|
2015-06-14 11:31:39 -06:00
|
|
|
var uri = source.uri;
|
|
|
|
if (uri && uri.match(/^\//)) {
|
|
|
|
uri = 'file://' + uri;
|
|
|
|
}
|
|
|
|
var isNetwork = !!(uri && uri.match(/^https?:/));
|
|
|
|
var isAsset = !!(uri && uri.match(/^(assets-library|file):/));
|
2015-03-30 23:07:55 -06:00
|
|
|
|
|
|
|
var resizeMode;
|
2015-04-05 11:17:03 -06:00
|
|
|
if (this.props.resizeMode === VideoResizeMode.stretch) {
|
|
|
|
resizeMode = NativeModules.VideoManager.ScaleToFill;
|
|
|
|
} else if (this.props.resizeMode === VideoResizeMode.contain) {
|
|
|
|
resizeMode = NativeModules.VideoManager.ScaleAspectFit;
|
2015-04-08 09:44:28 -06:00
|
|
|
} else if (this.props.resizeMode === VideoResizeMode.cover) {
|
2015-04-05 11:17:03 -06:00
|
|
|
resizeMode = NativeModules.VideoManager.ScaleAspectFill;
|
2015-03-30 23:07:55 -06:00
|
|
|
} else {
|
2015-04-05 11:17:03 -06:00
|
|
|
resizeMode = NativeModules.VideoManager.ScaleNone;
|
2015-03-30 23:07:55 -06:00
|
|
|
}
|
|
|
|
|
2015-09-16 15:36:17 -06:00
|
|
|
var nativeProps = extend({}, this.props, {
|
2015-03-30 23:07:55 -06:00
|
|
|
style,
|
2015-04-05 11:17:03 -06:00
|
|
|
resizeMode: resizeMode,
|
2015-04-08 00:49:14 -06:00
|
|
|
src: {
|
2015-06-14 11:31:39 -06:00
|
|
|
uri: uri,
|
2015-04-08 00:49:14 -06:00
|
|
|
isNetwork,
|
2015-04-08 09:44:28 -06:00
|
|
|
isAsset,
|
2015-04-08 00:49:14 -06:00
|
|
|
type: source.type || 'mp4'
|
|
|
|
},
|
2015-07-15 09:13:24 -06:00
|
|
|
onVideoLoad: this._onLoad,
|
|
|
|
onVideoProgress: this._onProgress,
|
|
|
|
onVideoEnd: this._onEnd,
|
2015-03-30 23:07:55 -06:00
|
|
|
});
|
|
|
|
|
2015-07-28 16:46:05 -06:00
|
|
|
return <RCTVideo ref={VIDEO_REF} {... nativeProps} />;
|
2015-03-30 23:07:55 -06:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2015-06-24 22:26:36 -06:00
|
|
|
var RCTVideo = requireNativeComponent('RCTVideo', Video);
|
2015-03-30 23:07:55 -06:00
|
|
|
|
|
|
|
var styles = StyleSheet.create({
|
|
|
|
base: {
|
|
|
|
overflow: 'hidden',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = Video;
|