react-native-video/Video.js

222 lines
5.8 KiB
JavaScript
Raw Normal View History

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