2016-01-31 20:35:18 -07:00
|
|
|
import React from 'react-native';
|
|
|
|
import VideoResizeMode from './VideoResizeMode.js';
|
|
|
|
|
2015-10-29 18:26:28 -06:00
|
|
|
const {
|
|
|
|
Component,
|
|
|
|
StyleSheet,
|
|
|
|
requireNativeComponent,
|
|
|
|
PropTypes,
|
2015-11-05 18:13:35 -07:00
|
|
|
NativeModules,
|
2015-11-17 20:15:07 -07:00
|
|
|
View,
|
2015-10-29 18:26:28 -06:00
|
|
|
} = React;
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
base: {
|
|
|
|
overflow: 'hidden',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2016-01-31 20:35:18 -07:00
|
|
|
export default class Video extends Component {
|
2015-10-29 18:26:28 -06:00
|
|
|
|
|
|
|
constructor(props, context) {
|
|
|
|
super(props, context);
|
|
|
|
this.seek = this.seek.bind(this);
|
2016-04-01 03:12:50 -06:00
|
|
|
this.presentFullscreenPlayer = this.presentFullscreenPlayer.bind(this);
|
|
|
|
this.dismissFullscreenPlayer = this.dismissFullscreenPlayer.bind(this);
|
2016-01-31 20:35:18 -07:00
|
|
|
this._assignRoot = this._assignRoot.bind(this);
|
2015-10-29 18:26:28 -06:00
|
|
|
this._onLoadStart = this._onLoadStart.bind(this);
|
|
|
|
this._onLoad = this._onLoad.bind(this);
|
|
|
|
this._onError = this._onError.bind(this);
|
|
|
|
this._onProgress = this._onProgress.bind(this);
|
|
|
|
this._onSeek = this._onSeek.bind(this);
|
|
|
|
this._onEnd = this._onEnd.bind(this);
|
2016-03-31 13:35:10 -06:00
|
|
|
this._onFullscreenPlayerWillPresent = this._onFullscreenPlayerWillPresent.bind(this);
|
|
|
|
this._onFullscreenPlayerDidPresent = this._onFullscreenPlayerDidPresent.bind(this);
|
|
|
|
this._onFullscreenPlayerWillDismiss = this._onFullscreenPlayerWillDismiss.bind(this);
|
|
|
|
this._onFullscreenPlayerDidDismiss = this._onFullscreenPlayerDidDismiss.bind(this);
|
2015-10-29 18:26:28 -06:00
|
|
|
}
|
|
|
|
|
2015-11-09 18:55:39 -07:00
|
|
|
setNativeProps(nativeProps) {
|
|
|
|
this._root.setNativeProps(nativeProps);
|
|
|
|
}
|
|
|
|
|
2015-11-11 14:34:41 -07:00
|
|
|
seek(time) {
|
2016-01-31 20:35:18 -07:00
|
|
|
this.setNativeProps({ seek: time });
|
|
|
|
}
|
|
|
|
|
2016-04-01 03:12:50 -06:00
|
|
|
presentFullscreenPlayer() {
|
2016-03-31 12:36:39 -06:00
|
|
|
this.setNativeProps({ fullscreen: true });
|
|
|
|
}
|
|
|
|
|
2016-04-01 03:12:50 -06:00
|
|
|
dismissFullscreenPlayer() {
|
|
|
|
this.setNativeProps({ fullscreen: false });
|
|
|
|
}
|
|
|
|
|
2016-01-31 20:35:18 -07:00
|
|
|
_assignRoot(component) {
|
|
|
|
this._root = component;
|
2015-11-11 14:34:41 -07:00
|
|
|
}
|
|
|
|
|
2015-11-17 20:15:07 -07:00
|
|
|
_onLoadStart(event) {
|
2016-01-31 20:35:18 -07:00
|
|
|
if (this.props.onLoadStart) {
|
|
|
|
this.props.onLoadStart(event.nativeEvent);
|
|
|
|
}
|
2015-11-17 20:15:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
_onLoad(event) {
|
2016-01-31 20:35:18 -07:00
|
|
|
if (this.props.onLoad) {
|
|
|
|
this.props.onLoad(event.nativeEvent);
|
|
|
|
}
|
2015-11-17 20:15:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
_onError(event) {
|
2016-01-31 20:35:18 -07:00
|
|
|
if (this.props.onError) {
|
|
|
|
this.props.onError(event.nativeEvent);
|
|
|
|
}
|
2015-11-17 20:15:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
_onProgress(event) {
|
2016-01-31 20:35:18 -07:00
|
|
|
if (this.props.onProgress) {
|
|
|
|
this.props.onProgress(event.nativeEvent);
|
|
|
|
}
|
2015-11-17 20:15:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
_onSeek(event) {
|
2016-01-31 20:35:18 -07:00
|
|
|
if (this.props.onSeek) {
|
|
|
|
this.props.onSeek(event.nativeEvent);
|
|
|
|
}
|
2015-11-17 20:15:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
_onEnd(event) {
|
2016-01-31 20:35:18 -07:00
|
|
|
if (this.props.onEnd) {
|
|
|
|
this.props.onEnd(event.nativeEvent);
|
|
|
|
}
|
2015-11-17 20:15:07 -07:00
|
|
|
}
|
|
|
|
|
2016-03-31 13:35:10 -06:00
|
|
|
_onFullscreenPlayerWillPresent(event) {
|
|
|
|
if (this.props.onFullscreenPlayerWillPresent) {
|
|
|
|
this.props.onFullscreenPlayerWillPresent(event.nativeEvent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onFullscreenPlayerDidPresent(event) {
|
2016-03-31 13:40:49 -06:00
|
|
|
if (this.props.onFullscreenPlayerDidPresent) {
|
2016-03-31 13:35:10 -06:00
|
|
|
this.props.onFullscreenPlayerDidPresent(event.nativeEvent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onFullscreenPlayerWillDismiss(event) {
|
2016-03-31 13:40:49 -06:00
|
|
|
if (this.props.onFullscreenPlayerWillDismiss) {
|
2016-03-31 13:35:10 -06:00
|
|
|
this.props.onFullscreenPlayerWillDismiss(event.nativeEvent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onFullscreenPlayerDidDismiss(event) {
|
|
|
|
if (this.props.onFullscreenPlayerDidDismiss) {
|
|
|
|
this.props.onFullscreenPlayerDidDismiss(event.nativeEvent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-29 18:26:28 -06:00
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
source,
|
|
|
|
resizeMode,
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
let uri = source.uri;
|
|
|
|
if (uri && uri.match(/^\//)) {
|
2016-01-31 20:35:18 -07:00
|
|
|
uri = `file://${uri}`;
|
2015-10-29 18:26:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
const isNetwork = !!(uri && uri.match(/^https?:/));
|
2016-03-12 01:29:21 -07:00
|
|
|
const isAsset = !!(uri && uri.match(/^(assets-library|file|content):/));
|
2015-10-29 18:26:28 -06:00
|
|
|
|
|
|
|
let nativeResizeMode;
|
|
|
|
if (resizeMode === VideoResizeMode.stretch) {
|
2015-11-05 18:13:35 -07:00
|
|
|
nativeResizeMode = NativeModules.UIManager.RCTVideo.Constants.ScaleToFill;
|
2015-10-29 18:26:28 -06:00
|
|
|
} else if (resizeMode === VideoResizeMode.contain) {
|
2015-11-05 18:13:35 -07:00
|
|
|
nativeResizeMode = NativeModules.UIManager.RCTVideo.Constants.ScaleAspectFit;
|
2015-10-29 18:26:28 -06:00
|
|
|
} else if (resizeMode === VideoResizeMode.cover) {
|
2015-11-05 18:13:35 -07:00
|
|
|
nativeResizeMode = NativeModules.UIManager.RCTVideo.Constants.ScaleAspectFill;
|
2015-10-29 18:26:28 -06:00
|
|
|
} else {
|
2015-11-05 18:13:35 -07:00
|
|
|
nativeResizeMode = NativeModules.UIManager.RCTVideo.Constants.ScaleNone;
|
2015-10-29 18:26:28 -06:00
|
|
|
}
|
|
|
|
|
2015-11-03 21:27:38 -07:00
|
|
|
const nativeProps = Object.assign({}, this.props);
|
2015-10-29 18:26:28 -06:00
|
|
|
Object.assign(nativeProps, {
|
2016-01-31 20:35:18 -07:00
|
|
|
style: [styles.base, nativeProps.style],
|
2015-10-29 18:26:28 -06:00
|
|
|
resizeMode: nativeResizeMode,
|
|
|
|
src: {
|
2016-01-31 20:35:18 -07:00
|
|
|
uri,
|
2015-10-29 18:26:28 -06:00
|
|
|
isNetwork,
|
|
|
|
isAsset,
|
2015-11-03 21:27:38 -07:00
|
|
|
type: source.type || 'mp4',
|
2015-10-29 18:26:28 -06:00
|
|
|
},
|
2015-11-11 14:34:41 -07:00
|
|
|
onVideoLoadStart: this._onLoadStart,
|
2015-10-29 18:26:28 -06:00
|
|
|
onVideoLoad: this._onLoad,
|
2015-11-11 14:34:41 -07:00
|
|
|
onVideoError: this._onError,
|
2015-10-29 18:26:28 -06:00
|
|
|
onVideoProgress: this._onProgress,
|
2015-11-11 14:34:41 -07:00
|
|
|
onVideoSeek: this._onSeek,
|
2015-10-29 18:26:28 -06:00
|
|
|
onVideoEnd: this._onEnd,
|
2016-03-31 13:35:10 -06:00
|
|
|
onVideoFullscreenPlayerWillPresent: this._onFullscreenPlayerWillPresent,
|
|
|
|
onVideoFullscreenPlayerDidPresent: this._onFullscreenPlayerDidPresent,
|
|
|
|
onVideoFullscreenPlayerWillDismiss: this._onFullscreenPlayerWillDismiss,
|
|
|
|
onVideoFullscreenPlayerDidDismiss: this._onFullscreenPlayerDidDismiss,
|
2015-10-29 18:26:28 -06:00
|
|
|
});
|
|
|
|
|
2015-11-09 18:55:39 -07:00
|
|
|
return (
|
|
|
|
<RCTVideo
|
2016-01-31 20:35:18 -07:00
|
|
|
ref={this._assignRoot}
|
|
|
|
{...nativeProps}
|
|
|
|
/>
|
2015-11-09 18:55:39 -07:00
|
|
|
);
|
2015-10-29 18:26:28 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Video.propTypes = {
|
|
|
|
/* Native only */
|
|
|
|
src: PropTypes.object,
|
|
|
|
seek: PropTypes.number,
|
2016-03-31 12:36:39 -06:00
|
|
|
fullscreen: PropTypes.bool,
|
2015-10-29 18:26:28 -06:00
|
|
|
|
|
|
|
/* Wrapper component */
|
|
|
|
source: PropTypes.object,
|
|
|
|
resizeMode: PropTypes.string,
|
|
|
|
repeat: PropTypes.bool,
|
|
|
|
paused: PropTypes.bool,
|
|
|
|
muted: PropTypes.bool,
|
|
|
|
volume: PropTypes.number,
|
|
|
|
rate: PropTypes.number,
|
2015-12-22 16:39:04 -07:00
|
|
|
controls: PropTypes.bool,
|
|
|
|
currentTime: PropTypes.number,
|
2015-10-29 18:26:28 -06:00
|
|
|
onLoadStart: PropTypes.func,
|
|
|
|
onLoad: PropTypes.func,
|
|
|
|
onError: PropTypes.func,
|
|
|
|
onProgress: PropTypes.func,
|
2015-11-11 14:34:41 -07:00
|
|
|
onSeek: PropTypes.func,
|
2015-10-29 18:26:28 -06:00
|
|
|
onEnd: PropTypes.func,
|
2016-03-31 13:35:10 -06:00
|
|
|
onFullscreenPlayerWillPresent: PropTypes.func,
|
|
|
|
onFullscreenPlayerDidPresent: PropTypes.func,
|
|
|
|
onFullscreenPlayerWillDismiss: PropTypes.func,
|
|
|
|
onFullscreenPlayerDidDismiss: PropTypes.func,
|
2016-01-31 20:35:18 -07:00
|
|
|
|
2015-10-29 18:26:28 -06:00
|
|
|
/* Required by react-native */
|
|
|
|
scaleX: React.PropTypes.number,
|
|
|
|
scaleY: React.PropTypes.number,
|
|
|
|
translateX: React.PropTypes.number,
|
|
|
|
translateY: React.PropTypes.number,
|
|
|
|
rotation: React.PropTypes.number,
|
2015-12-07 02:03:21 -07:00
|
|
|
...View.propTypes,
|
2015-10-29 18:26:28 -06:00
|
|
|
};
|
|
|
|
|
2015-11-03 21:27:38 -07:00
|
|
|
const RCTVideo = requireNativeComponent('RCTVideo', Video, {
|
2015-10-29 18:26:28 -06:00
|
|
|
nativeOnly: {
|
|
|
|
src: true,
|
2016-01-31 20:35:18 -07:00
|
|
|
seek: true,
|
2016-04-01 03:12:50 -06:00
|
|
|
fullscreen: true,
|
2015-10-29 18:26:28 -06:00
|
|
|
},
|
|
|
|
});
|