forked from TheWidlarzGroup/react-native-video
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Video.js
153 lines (130 loc) · 3.65 KB
/
Video.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const React = require('react-native');
const {
Component,
StyleSheet,
requireNativeComponent,
PropTypes,
NativeModules,
View,
} = React;
const VideoResizeMode = require('./VideoResizeMode');
const styles = StyleSheet.create({
base: {
overflow: 'hidden',
},
});
class Video extends Component {
constructor(props, context) {
super(props, context);
this.seek = this.seek.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);
}
setNativeProps(nativeProps) {
this._root.setNativeProps(nativeProps);
}
seek(time) {
this.setNativeProps({ seek: parseFloat(time) });
}
_onLoadStart(event) {
this.props.onLoadStart && this.props.onLoadStart(event.nativeEvent);
}
_onLoad(event) {
this.props.onLoad && this.props.onLoad(event.nativeEvent);
}
_onError(event) {
this.props.onError && this.props.onError(event.nativeEvent);
}
_onProgress(event) {
this.props.onProgress && this.props.onProgress(event.nativeEvent);
}
_onSeek(event) {
this.props.onSeek && this.props.onSeek(event.nativeEvent);
}
_onEnd(event) {
this.props.onEnd && this.props.onEnd(event.nativeEvent);
}
render() {
const {
style,
source,
ref,
resizeMode,
} = this.props;
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):/));
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, style],
resizeMode: nativeResizeMode,
src: {
uri: 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,
});
return (
<RCTVideo
ref={ component => this._root = component }
{...nativeProps} />
);
}
}
Video.propTypes = {
/* Native only */
src: PropTypes.object,
seek: PropTypes.number,
/* Wrapper component */
source: PropTypes.object,
resizeMode: PropTypes.string,
repeat: PropTypes.bool,
paused: PropTypes.bool,
muted: PropTypes.bool,
volume: PropTypes.number,
rate: PropTypes.number,
onLoadStart: PropTypes.func,
onLoad: PropTypes.func,
onError: PropTypes.func,
onProgress: PropTypes.func,
onSeek: PropTypes.func,
onEnd: 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,
...View.propTypes,
};
const RCTVideo = requireNativeComponent('RCTVideo', Video, {
nativeOnly: {
src: true,
seek: true,
},
});
module.exports = Video;