react-native-omplayer
Version:
video player for react-native
224 lines (189 loc) • 7.42 kB
JavaScript
/**
* Created by lcg on 2017/1/20.
*/
import React, { Component } from 'react';
import { View, requireNativeComponent, StyleSheet } from 'react-native';
export default class OMPlayer extends Component {
constructor(props){
super(props);
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);
this._onReadyForDisplay = this._onReadyForDisplay.bind(this);
this._onPlaybackStalled = this._onPlaybackStalled.bind(this);
this._onPlaybackResume = this._onPlaybackResume.bind(this);
this._onPlaybackRateChange = this._onPlaybackRateChange.bind(this);
}
_onLoadStart(event) {
// console.log('onLoadStart',event.nativeEvent);
if (this.props.onLoadStart) {
this.props.onLoadStart(event.nativeEvent);
}
}
_onLoad(event) {
console.log('onLoad',event.nativeEvent);
if (this.props.onLoad) {
this.props.onLoad(event.nativeEvent);
}
}
_onError(event) {
// console.log('onError',event.nativeEvent);
if (this.props.onError) {
this.props.onError(event.nativeEvent);
}
}
_onProgress(event) {
// console.log('onProgress',event.nativeEvent);
if (this.props.onProgress) {
this.props.onProgress(event.nativeEvent);
}
}
_onSeek(event) {
// console.log('onSeek',event.nativeEvent);
if (this.props.onSeek) {
this.props.onSeek(event.nativeEvent);
}
}
_onEnd(event) {
// console.log('onEnd',event.nativeEvent);
if (this.props.onEnd) {
this.props.onEnd(event.nativeEvent);
}
}
_onFullscreenPlayerWillPresent(event) {
// if (this.props.onFullscreenPlayerWillPresent) {
// this.props.onFullscreenPlayerWillPresent(event.nativeEvent);
// }
}
_onFullscreenPlayerDidPresent(event) {
// if (this.props.onFullscreenPlayerDidPresent) {
// this.props.onFullscreenPlayerDidPresent(event.nativeEvent);
// }
}
_onFullscreenPlayerWillDismiss(event) {
// if (this.props.onFullscreenPlayerWillDismiss) {
// this.props.onFullscreenPlayerWillDismiss(event.nativeEvent);
// }
}
_onFullscreenPlayerDidDismiss(event) {
// if (this.props.onFullscreenPlayerDidDismiss) {
// this.props.onFullscreenPlayerDidDismiss(event.nativeEvent);
// }
}
_onReadyForDisplay(event) {
// console.log('onReadyForDisplay',event.nativeEvent);
if (this.props.onReadyForDisplay) {
this.props.onReadyForDisplay(event.nativeEvent);
}
}
_onPlaybackStalled(event) {
// console.log('onPlaybackStalled',event.nativeEvent);
if (this.props.onPlaybackStalled) {
this.props.onPlaybackStalled(event.nativeEvent);
}
}
_onPlaybackResume(event) {
// console.log('onPlaybackResume',event.nativeEvent);
if (this.props.onPlaybackResume) {
this.props.onPlaybackResume(event.nativeEvent);
}
}
_onPlaybackRateChange(event) {
// console.log('onPlaybackRateChange',event.nativeEvent);
if (this.props.onPlaybackRateChange) {
this.props.onPlaybackRateChange(event.nativeEvent);
}
}
seek(seek){
this._root && this._root.setNativeProps({ seek: seek });
}
render(){
let uri = this.props.source.uri;
if (uri && uri.match(/^\//)) {
uri = `file://${uri}`;
}
const nativeProps = Object.assign({}, this.props,{
style: [styles.base,this.props.style],
src: uri,
onOMVideoLoadStart: this._onLoadStart,
onOMVideoLoad: this._onLoad,
onOMVideoError: this._onError,
onOMVideoProgress: this._onProgress,
onOMVideoSeek: this._onSeek,
onOMVideoEnd: this._onEnd,
onOMVideoFullscreenPlayerWillPresent: this._onFullscreenPlayerWillPresent,
onOMVideoFullscreenPlayerDidPresent: this._onFullscreenPlayerDidPresent,
onOMVideoFullscreenPlayerWillDismiss: this._onFullscreenPlayerWillDismiss,
onOMVideoFullscreenPlayerDidDismiss: this._onFullscreenPlayerDidDismiss,
onOMReadyForDisplay: this._onReadyForDisplay,
onOMPlaybackStalled: this._onPlaybackStalled,
onOMPlaybackResume: this._onPlaybackResume,
onOMPlaybackRateChange: this._onPlaybackRateChange,
ref: (nativePlayer) => { this._root = nativePlayer }
});
return <OMPlayerView {...nativeProps}/>
}
}
OMPlayer.propTypes = {
...View.propTypes,
source: React.PropTypes.object,
resizeMode: React.PropTypes.string,
repeat: React.PropTypes.bool,
paused: React.PropTypes.bool,
muted: React.PropTypes.bool,
controls: React.PropTypes.bool, //not implement
volume: React.PropTypes.number,
rate: React.PropTypes.number,
seek: React.PropTypes.number,
currentTime: React.PropTypes.number,
fullscreen: React.PropTypes.bool, //not implement
onOMVideoLoadStart: React.PropTypes.func,
onOMVideoLoad: React.PropTypes.func,
onOMVideoError: React.PropTypes.func,
onOMVideoProgress: React.PropTypes.func,
onOMVideoSeek: React.PropTypes.func,
onOMVideoEnd: React.PropTypes.func,
onOMVideoFullscreenPlayerWillPresent: React.PropTypes.func, //not implement
onOMVideoFullscreenPlayerDidPresent: React.PropTypes.func, //not implement
onOMVideoFullscreenPlayerWillDismiss: React.PropTypes.func, //not implement
onOMVideoFullscreenPlayerDidDismiss: React.PropTypes.func, //not implement
onOMReadyForDisplay: React.PropTypes.func,
onOMPlaybackStalled: React.PropTypes.func,
onOMPlaybackResume: React.PropTypes.func,
onOMPlaybackRateChange: React.PropTypes.func
};
const OMPlayerView = requireNativeComponent('RCTOMPlayer',OMPlayer,{
nativeOnly: {
src: true,
seek: true,
fullscreen: true,
controls: true,
onOMVideoLoadStart: true,
onOMVideoLoad: true,
onOMVideoError: true,
onOMVideoProgress: true,
onOMVideoSeek: true,
onOMVideoEnd: true,
onOMVideoFullscreenPlayerWillPresent: true,
onOMVideoFullscreenPlayerDidPresent: true,
onOMVideoFullscreenPlayerWillDismiss: true,
onOMVideoFullscreenPlayerDidDismiss: true,
onOMReadyForDisplay: true,
onOMPlaybackStalled: true,
onOMPlaybackResume: true,
onOMPlaybackRateChange: true,
}
});
const styles = StyleSheet.create({
base: {
backgroundColor: 'black',
overflow: 'hidden'
}
});