react-native-video/Video.js

275 lines
7.1 KiB
JavaScript
Raw Normal View History

2016-06-22 07:52:12 -06:00
import React, {Component, PropTypes} from 'react';
import {StyleSheet, requireNativeComponent, NativeModules, View, Image} from 'react-native';
2016-06-22 07:52:12 -06:00
import resolveAssetSource from 'react-native/Libraries/Image/resolveAssetSource';
import VideoResizeMode from './VideoResizeMode.js';
const styles = StyleSheet.create({
base: {
overflow: 'hidden',
},
});
export default class Video extends Component {
constructor(props) {
super(props);
this.state = {
showPoster: true,
};
}
2015-11-09 18:55:39 -07:00
setNativeProps(nativeProps) {
this._root.setNativeProps(nativeProps);
}
2016-06-22 08:28:54 -06:00
seek = (time) => {
this.setNativeProps({ seek: time });
2016-06-22 08:28:54 -06:00
};
2016-06-22 08:28:54 -06:00
presentFullscreenPlayer = () => {
this.setNativeProps({ fullscreen: true });
2016-06-22 08:28:54 -06:00
};
2016-06-22 08:28:54 -06:00
dismissFullscreenPlayer = () => {
this.setNativeProps({ fullscreen: false });
2016-06-22 08:28:54 -06:00
};
2016-06-22 08:28:54 -06:00
_assignRoot = (component) => {
this._root = component;
2016-06-22 08:28:54 -06:00
};
2016-06-22 08:28:54 -06:00
_onLoadStart = (event) => {
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) => {
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) => {
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) => {
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) => {
if (this.state.showPoster) {
this.setState({showPoster: false});
}
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) => {
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) => {
if (this.props.onFullscreenPlayerWillPresent) {
this.props.onFullscreenPlayerWillPresent(event.nativeEvent);
}
2016-06-22 08:28:54 -06:00
};
2016-06-22 08:28:54 -06:00
_onFullscreenPlayerDidPresent = (event) => {
2016-03-31 13:40:49 -06:00
if (this.props.onFullscreenPlayerDidPresent) {
this.props.onFullscreenPlayerDidPresent(event.nativeEvent);
}
2016-06-22 08:28:54 -06:00
};
2016-06-22 08:28:54 -06:00
_onFullscreenPlayerWillDismiss = (event) => {
2016-03-31 13:40:49 -06:00
if (this.props.onFullscreenPlayerWillDismiss) {
this.props.onFullscreenPlayerWillDismiss(event.nativeEvent);
}
2016-06-22 08:28:54 -06:00
};
2016-06-22 08:28:54 -06:00
_onFullscreenPlayerDidDismiss = (event) => {
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) => {
if (this.state.showPoster && (event.nativeEvent.playbackRate !== 0)) {
this.setState({showPoster: false});
}
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
render() {
2016-06-22 07:52:12 -06:00
const resizeMode = this.props.resizeMode;
const source = resolveAssetSource(this.props.source) || {};
let uri = source.uri;
if (uri && uri.match(/^\//)) {
uri = `file://${uri}`;
}
const isNetwork = !!(uri && uri.match(/^https?:/));
const isAsset = !!(uri && uri.match(/^(assets-library|file|content|ms-appx|ms-appdata):/));
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',
mainVer: source.mainVer || 0,
patchVer: source.patchVer || 0,
},
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,
2016-04-28 06:42:44 -06:00
onReadyForDisplay: this._onReadyForDisplay,
onPlaybackStalled: this._onPlaybackStalled,
onPlaybackResume: this._onPlaybackResume,
onPlaybackRateChange: this._onPlaybackRateChange,
});
if (this.props.poster && this.state.showPoster) {
const posterStyle = {
position: 'absolute',
left: 0,
top: 0,
right: 0,
bottom: 0,
resizeMode: 'contain',
};
return (
<View style={nativeProps.style}>
<RCTVideo
ref={this._assignRoot}
{...nativeProps}
/>
<Image
style={posterStyle}
source={{uri: this.props.poster}}
/>
</View>
);
}
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 */
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
]),
resizeMode: PropTypes.string,
poster: PropTypes.string,
repeat: PropTypes.bool,
paused: PropTypes.bool,
muted: PropTypes.bool,
volume: PropTypes.number,
rate: PropTypes.number,
playInBackground: PropTypes.bool,
playWhenInactive: PropTypes.bool,
2015-12-22 16:39:04 -07:00
controls: PropTypes.bool,
currentTime: PropTypes.number,
progressUpdateInterval: 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,
2016-04-28 06:42:44 -06:00
onReadyForDisplay: PropTypes.func,
onPlaybackStalled: PropTypes.func,
onPlaybackResume: PropTypes.func,
onPlaybackRateChange: PropTypes.func,
/* 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,
};
const RCTVideo = requireNativeComponent('RCTVideo', Video, {
nativeOnly: {
src: true,
seek: true,
fullscreen: true,
},
});