2016-06-22 07:52:12 -06:00
|
|
|
import React, {Component, PropTypes} from 'react';
|
|
|
|
import {StyleSheet, requireNativeComponent, NativeModules, View} from 'react-native';
|
|
|
|
import resolveAssetSource from 'react-native/Libraries/Image/resolveAssetSource';
|
2016-01-31 20:35:18 -07:00
|
|
|
import VideoResizeMode from './VideoResizeMode.js';
|
|
|
|
|
2015-10-29 18:26:28 -06:00
|
|
|
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
|
|
|
|
2015-11-09 18:55:39 -07:00
|
|
|
setNativeProps(nativeProps) {
|
|
|
|
this._root.setNativeProps(nativeProps);
|
|
|
|
}
|
|
|
|
|
2016-06-22 08:28:54 -06:00
|
|
|
seek = (time) => {
|
2016-01-31 20:35:18 -07:00
|
|
|
this.setNativeProps({ seek: time });
|
2016-06-22 08:28:54 -06:00
|
|
|
};
|
2016-01-31 20:35:18 -07:00
|
|
|
|
2016-06-22 08:28:54 -06:00
|
|
|
presentFullscreenPlayer = () => {
|
2016-03-31 12:36:39 -06:00
|
|
|
this.setNativeProps({ fullscreen: true });
|
2016-06-22 08:28:54 -06:00
|
|
|
};
|
2016-03-31 12:36:39 -06:00
|
|
|
|
2016-06-22 08:28:54 -06:00
|
|
|
dismissFullscreenPlayer = () => {
|
2016-04-01 03:12:50 -06:00
|
|
|
this.setNativeProps({ fullscreen: false });
|
2016-06-22 08:28:54 -06:00
|
|
|
};
|
2016-04-01 03:12:50 -06:00
|
|
|
|
2016-06-22 08:28:54 -06:00
|
|
|
_assignRoot = (component) => {
|
2016-01-31 20:35:18 -07:00
|
|
|
this._root = component;
|
2016-06-22 08:28:54 -06:00
|
|
|
};
|
2015-11-11 14:34:41 -07:00
|
|
|
|
2016-06-22 08:28:54 -06:00
|
|
|
_onLoadStart = (event) => {
|
2016-01-31 20:35:18 -07:00
|
|
|
if (this.props.onLoadStart) {
|
|
|
|
this.props.onLoadStart(event.nativeEvent);
|
|
|
|
}
|
2016-06-22 08:28:54 -06:00
|
|
|
};
|
2015-11-17 20:15:07 -07:00
|
|
|
|
2016-06-22 08:28:54 -06:00
|
|
|
_onLoad = (event) => {
|
2016-01-31 20:35:18 -07:00
|
|
|
if (this.props.onLoad) {
|
|
|
|
this.props.onLoad(event.nativeEvent);
|
|
|
|
}
|
2016-06-22 08:28:54 -06:00
|
|
|
};
|
2015-11-17 20:15:07 -07:00
|
|
|
|
2016-06-22 08:28:54 -06:00
|
|
|
_onError = (event) => {
|
2016-01-31 20:35:18 -07:00
|
|
|
if (this.props.onError) {
|
|
|
|
this.props.onError(event.nativeEvent);
|
|
|
|
}
|
2016-06-22 08:28:54 -06:00
|
|
|
};
|
2015-11-17 20:15:07 -07:00
|
|
|
|
2016-06-22 08:28:54 -06:00
|
|
|
_onProgress = (event) => {
|
2016-01-31 20:35:18 -07:00
|
|
|
if (this.props.onProgress) {
|
|
|
|
this.props.onProgress(event.nativeEvent);
|
|
|
|
}
|
2016-06-22 08:28:54 -06:00
|
|
|
};
|
2015-11-17 20:15:07 -07:00
|
|
|
|
2016-06-22 08:28:54 -06:00
|
|
|
_onSeek = (event) => {
|
2016-01-31 20:35:18 -07:00
|
|
|
if (this.props.onSeek) {
|
|
|
|
this.props.onSeek(event.nativeEvent);
|
|
|
|
}
|
2016-06-22 08:28:54 -06:00
|
|
|
};
|
2015-11-17 20:15:07 -07:00
|
|
|
|
2016-06-22 08:28:54 -06:00
|
|
|
_onEnd = (event) => {
|
2016-01-31 20:35:18 -07:00
|
|
|
if (this.props.onEnd) {
|
|
|
|
this.props.onEnd(event.nativeEvent);
|
|
|
|
}
|
2016-06-22 08:28:54 -06:00
|
|
|
};
|
2015-11-17 20:15:07 -07:00
|
|
|
|
2016-06-22 08:28:54 -06:00
|
|
|
_onFullscreenPlayerWillPresent = (event) => {
|
2016-03-31 13:35:10 -06:00
|
|
|
if (this.props.onFullscreenPlayerWillPresent) {
|
|
|
|
this.props.onFullscreenPlayerWillPresent(event.nativeEvent);
|
|
|
|
}
|
2016-06-22 08:28:54 -06:00
|
|
|
};
|
2016-03-31 13:35:10 -06:00
|
|
|
|
2016-06-22 08:28:54 -06:00
|
|
|
_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);
|
|
|
|
}
|
2016-06-22 08:28:54 -06:00
|
|
|
};
|
2016-03-31 13:35:10 -06:00
|
|
|
|
2016-06-22 08:28:54 -06:00
|
|
|
_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);
|
|
|
|
}
|
2016-06-22 08:28:54 -06:00
|
|
|
};
|
2016-03-31 13:35:10 -06:00
|
|
|
|
2016-06-22 08:28:54 -06:00
|
|
|
_onFullscreenPlayerDidDismiss = (event) => {
|
2016-03-31 13:35:10 -06:00
|
|
|
if (this.props.onFullscreenPlayerDidDismiss) {
|
|
|
|
this.props.onFullscreenPlayerDidDismiss(event.nativeEvent);
|
|
|
|
}
|
2016-06-22 08:28:54 -06:00
|
|
|
};
|
2015-11-17 20:15:07 -07:00
|
|
|
|
2016-06-22 08:28:54 -06:00
|
|
|
_onReadyForDisplay = (event) => {
|
2016-04-28 06:42:44 -06:00
|
|
|
if (this.props.onReadyForDisplay) {
|
|
|
|
this.props.onReadyForDisplay(event.nativeEvent);
|
|
|
|
}
|
2016-06-22 08:28:54 -06:00
|
|
|
};
|
2016-04-28 06:42:44 -06:00
|
|
|
|
2016-06-22 08:28:54 -06:00
|
|
|
_onPlaybackStalled = (event) => {
|
2016-04-28 06:42:44 -06:00
|
|
|
if (this.props.onPlaybackStalled) {
|
|
|
|
this.props.onPlaybackStalled(event.nativeEvent);
|
|
|
|
}
|
2016-06-22 08:28:54 -06:00
|
|
|
};
|
2016-04-28 06:42:44 -06:00
|
|
|
|
2016-06-22 08:28:54 -06:00
|
|
|
_onPlaybackResume = (event) => {
|
2016-04-28 06:42:44 -06:00
|
|
|
if (this.props.onPlaybackResume) {
|
|
|
|
this.props.onPlaybackResume(event.nativeEvent);
|
|
|
|
}
|
2016-06-22 08:28:54 -06:00
|
|
|
};
|
2016-04-28 06:42:44 -06:00
|
|
|
|
2016-06-22 08:28:54 -06:00
|
|
|
_onPlaybackRateChange = (event) => {
|
2016-04-28 06:42:44 -06:00
|
|
|
if (this.props.onPlaybackRateChange) {
|
|
|
|
this.props.onPlaybackRateChange(event.nativeEvent);
|
|
|
|
}
|
2016-06-22 08:28:54 -06:00
|
|
|
};
|
2016-04-28 06:42:44 -06:00
|
|
|
|
2015-10-29 18:26:28 -06:00
|
|
|
render() {
|
2016-06-22 07:52:12 -06:00
|
|
|
const resizeMode = this.props.resizeMode;
|
|
|
|
const source = resolveAssetSource(this.props.source) || {};
|
2015-10-29 18:26:28 -06:00
|
|
|
|
|
|
|
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,
|
2016-04-28 06:42:44 -06:00
|
|
|
onReadyForDisplay: this._onReadyForDisplay,
|
|
|
|
onPlaybackStalled: this._onPlaybackStalled,
|
|
|
|
onPlaybackResume: this._onPlaybackResume,
|
|
|
|
onPlaybackRateChange: this._onPlaybackRateChange,
|
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 */
|
2016-06-22 07:52:12 -06:00
|
|
|
source: PropTypes.oneOfType([
|
|
|
|
PropTypes.shape({
|
|
|
|
uri: PropTypes.string
|
|
|
|
}),
|
|
|
|
// Opaque type returned by require('./video.mp4')
|
|
|
|
PropTypes.number
|
|
|
|
]),
|
2015-10-29 18:26:28 -06:00
|
|
|
resizeMode: PropTypes.string,
|
|
|
|
repeat: PropTypes.bool,
|
|
|
|
paused: PropTypes.bool,
|
|
|
|
muted: PropTypes.bool,
|
|
|
|
volume: PropTypes.number,
|
|
|
|
rate: PropTypes.number,
|
2015-12-16 05:53:04 -07:00
|
|
|
playInBackground: PropTypes.bool,
|
2016-04-29 05:55:34 -06:00
|
|
|
playWhenInactive: PropTypes.bool,
|
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-04-28 06:42:44 -06:00
|
|
|
onReadyForDisplay: PropTypes.func,
|
|
|
|
onPlaybackStalled: PropTypes.func,
|
|
|
|
onPlaybackResume: PropTypes.func,
|
|
|
|
onPlaybackRateChange: PropTypes.func,
|
2015-10-29 18:26:28 -06:00
|
|
|
|
|
|
|
/* Required by react-native */
|
2016-06-22 07:52:12 -06:00
|
|
|
scaleX: PropTypes.number,
|
|
|
|
scaleY: PropTypes.number,
|
|
|
|
translateX: PropTypes.number,
|
|
|
|
translateY: PropTypes.number,
|
|
|
|
rotation: 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,
|
|
|
|
seek: true,
|
2016-04-01 03:12:50 -06:00
|
|
|
fullscreen: true,
|
2015-10-29 18:26:28 -06:00
|
|
|
},
|
|
|
|
});
|