react-native-video/Video.ios.js

110 lines
2.7 KiB
JavaScript
Raw Normal View History

var React = require('react-native');
var { StyleSheet, requireNativeComponent, PropTypes, NativeModules, } = React;
2015-03-30 23:07:55 -06:00
var VideoResizeMode = require('./VideoResizeMode');
var merge = require('merge');
var VIDEO_REF = 'video';
2015-03-30 23:07:55 -06:00
var Video = React.createClass({
propTypes: {
/* Native only */
src: PropTypes.object,
seek: PropTypes.number,
/* Wrapper component */
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
},
setNativeProps(props) {
this.refs[VIDEO_REF].setNativeProps(props);
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() {
var style = [styles.base, this.props.style];
2015-03-30 23:07:55 -06:00
var source = this.props.source;
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;
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: uri,
isNetwork,
2015-04-08 09:44:28 -06:00
isAsset,
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
});
return <RCTVideo ref={VIDEO_REF} {... nativeProps} />;
2015-03-30 23:07:55 -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;