react-native-video/Video.ios.js

116 lines
3.3 KiB
JavaScript
Raw Normal View History

var React = require('react-native');
2015-03-30 23:07:55 -06:00
var NativeModules = require('NativeModules');
2015-05-14 19:10:41 -06:00
var ReactNativeViewAttributes = require('ReactNativeViewAttributes');
2015-03-30 23:07:55 -06:00
var StyleSheet = require('StyleSheet');
2015-05-14 19:10:41 -06:00
var createReactIOSNativeComponentClass = require('createReactNativeComponentClass');
2015-03-30 23:07:55 -06:00
var PropTypes = require('ReactPropTypes');
var StyleSheetPropType = require('StyleSheetPropType');
var VideoResizeMode = require('./VideoResizeMode');
var VideoStylePropTypes = require('./VideoStylePropTypes');
var NativeMethodsMixin = require('NativeMethodsMixin');
var flattenStyle = require('flattenStyle');
var merge = require('merge');
var deepDiffer = require('deepDiffer');
2015-03-30 23:07:55 -06:00
var Video = React.createClass({
propTypes: {
style: StyleSheetPropType(VideoStylePropTypes),
source: PropTypes.object,
resizeMode: PropTypes.string,
2015-03-31 00:29:15 -06:00
repeat: PropTypes.bool,
paused: PropTypes.bool,
muted: PropTypes.bool,
volume: PropTypes.number,
rate: PropTypes.number,
onLoadStart: PropTypes.func,
onLoad: PropTypes.func,
onError: PropTypes.func,
onProgress: PropTypes.func,
2015-04-10 21:40:13 -06:00
onEnd: PropTypes.func,
2015-03-30 23:07:55 -06:00
},
mixins: [NativeMethodsMixin],
viewConfig: {
uiViewClassName: 'UIView',
2015-05-14 19:10:41 -06:00
validAttributes: ReactNativeViewAttributes.UIView
2015-03-30 23:07:55 -06:00
},
_onLoadStart(event) {
this.props.onLoadStart && this.props.onLoadStart(event.nativeEvent);
},
_onLoad(event) {
this.props.onLoad && this.props.onLoad(event.nativeEvent);
},
_onError(event) {
this.props.onError && this.props.onError(event.nativeEvent);
},
_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);
},
seek(time) {
2015-04-09 09:12:54 -06:00
this.setNativeProps({seek: parseFloat(time)});
},
2015-04-10 21:40:13 -06:00
_onEnd(event) {
this.props.onEnd && this.props.onEnd(event.nativeEvent);
},
render() {
2015-03-30 23:07:55 -06:00
var style = flattenStyle([styles.base, this.props.style]);
var source = this.props.source;
var isNetwork = !!(source.uri && source.uri.match(/^https?:/));
2015-04-08 09:44:28 -06:00
var isAsset = !!(source.uri && source.uri.match(/^assets-library:/));
2015-03-30 23:07:55 -06:00
var resizeMode;
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) {
resizeMode = NativeModules.VideoManager.ScaleAspectFill;
2015-03-30 23:07:55 -06:00
} else {
resizeMode = NativeModules.VideoManager.ScaleNone;
2015-03-30 23:07:55 -06:00
}
var nativeProps = merge(this.props, {
style,
resizeMode: resizeMode,
src: {
uri: source.uri,
isNetwork,
2015-04-08 09:44:28 -06:00
isAsset,
type: source.type || 'mp4'
},
onLoad: this._onLoad,
onProgress: this._onProgress,
2015-04-10 21:40:13 -06:00
onEnd: this._onEnd,
2015-03-30 23:07:55 -06:00
});
2015-04-09 09:12:54 -06:00
return <RCTVideo {... nativeProps} />;
2015-03-30 23:07:55 -06:00
},
});
var RCTVideo = createReactIOSNativeComponentClass({
2015-05-14 19:10:41 -06:00
validAttributes: merge(ReactNativeViewAttributes.UIView,
{src: {diff: deepDiffer}, resizeMode: true, repeat: true,
seek: true, paused: true, muted: true, volume: true, rate: true}),
2015-03-30 23:07:55 -06:00
uiViewClassName: 'RCTVideo',
});
var styles = StyleSheet.create({
base: {
overflow: 'hidden',
},
});
module.exports = Video;