-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathindex.ios.js
250 lines (212 loc) · 5.71 KB
/
index.ios.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**
* Sample React Native App
* https://github.com./facebook/react-native
*/
'use strict';
var React = require('react-native');
var {
AppRegistry,
View,
Animated,
StatusBarIOS,
TouchableOpacity,
TouchableWithoutFeedback,
Text,
NavigatorIOS
} = React;
var styles = require('./style');
var screen = require('Dimensions').get('window');
var Recorder = require('react-native-screcorder');
var Video = require('react-native-video');
/*********** RECORDER COMPONENT ***********/
var Record = React.createClass({
getInitialState: function() {
return {
device: "front",
recording: false,
nbSegments: 0,
barPosition: new Animated.Value(0),
currentDuration: 0,
maxDuration: 3000,
limitReached: false,
config: {
flashMode: Recorder.constants.SCFlashModeOff,
video: {
enabled: true,
format: 'MPEG4',
},
}
}
},
componentDidMount: function() {
StatusBarIOS.setHidden(true, "slide");
},
/*
* PRIVATE METHODS
*/
startBarAnimation: function() {
this.animRunning = true;
this.animBar = Animated.timing(
this.state.barPosition,
{
toValue: screen.width,
duration: this.state.maxDuration - this.state.currentDuration
}
);
this.animBar.start(() => {
// The video duration limit has been reached
if (this.animRunning) {
this.finish();
}
});
},
resetBarAnimation: function() {
Animated.spring(this.state.barPosition, {toValue: 0}).start();
},
stopBarAnimation: function() {
this.animRunning = false;
if (this.animBar)
this.animBar.stop();
},
/*
* PUBLIC METHODS
*/
record: function() {
if (this.state.limitReached) return;
this.refs.recorder.record();
this.startBarAnimation();
this.setState({recording: true});
},
pause: function(limitReached) {
if (!this.state.recording) return;
this.refs.recorder.pause();
this.stopBarAnimation();
this.setState({recording: false, nbSegments: ++this.state.nbSegments});
},
finish: function() {
this.stopBarAnimation();
this.refs.recorder.pause();
this.setState({recording: false, limitReached: true, nbSegments: ++this.state.nbSegments});
},
reset: function() {
this.resetBarAnimation();
this.refs.recorder.removeAllSegments();
this.setState({
recording: false,
nbSegments: 0,
currentDuration: 0,
limitReached: false
});
},
preview: function() {
this.refs.recorder.save((err, url) => {
console.log('url = ', url);
this.props.navigator.push({component: Preview, passProps: {video: url}});
});
},
setDevice: function() {
var device = (this.state.device == "front") ? "back" : "front";
this.setState({device: device});
},
toggleFlash: function() {
if (this.state.config.flashMode == Recorder.constants.SCFlashModeOff) {
this.state.config.flashMode = Recorder.constants.SCFlashModeLight;
} else {
this.state.config.flashMode = Recorder.constants.SCFlashModeOff;
}
this.setState({config: this.state.config});
},
/*
* EVENTS
*/
onRecordDone: function() {
this.setState({nbSegments: 0});
},
onNewSegment: function(segment) {
console.log('segment = ', segment);
this.state.currentDuration += segment.duration * 1000;
},
/*
* RENDER METHODS
*/
renderBar: function() {
return (
<View style={styles.barWrapper}>
<Animated.View style={[styles.barGauge, {width: this.state.barPosition}]}/>
</View>
);
},
render: function() {
var bar = this.renderBar();
var control = null;
if (!this.state.limitReached) {
control = (
<TouchableOpacity onPressIn={this.record} onPressOut={this.pause} style={styles.controlBtn}>
<Text>Record</Text>
</TouchableOpacity>
);
}
return (
<Recorder
ref="recorder"
config={this.state.config}
device={this.state.device}
onNewSegment={this.onNewSegment}
style={styles.wrapper}>
{bar}
<View style={styles.infoBtn}>
<Text style={styles.infoBtnText}>{this.state.nbSegments}</Text>
</View>
<View style={styles.controls}>
{control}
<TouchableOpacity onPressIn={this.reset} style={styles.controlBtn}>
<Text>Reset</Text>
</TouchableOpacity>
<TouchableOpacity onPress={this.preview} style={styles.controlBtn}>
<Text>Preview</Text>
</TouchableOpacity>
<TouchableOpacity onPress={this.toggleFlash} style={styles.controlBtn}>
<Text>Flash</Text>
</TouchableOpacity>
<TouchableOpacity onPress={this.setDevice} style={styles.controlBtn}>
<Text>Switch</Text>
</TouchableOpacity>
</View>
</Recorder>
);
}
});
/*********** PREVIEW COMPONENT ***********/
var Preview = React.createClass({
getInitialState: function() {
return {
paused: false
};
},
goBack: function() {
this.setState({paused: true});
this.props.navigator.pop();
},
render: function() {
return (
<TouchableWithoutFeedback onPress={this.goBack}>
<Video
source={{uri: this.props.video}}
style={styles.wrapper}
muted={false}
resizeMode="cover"
paused={this.state.paused}
repeat={true}/>
</TouchableWithoutFeedback>
);
}
});
/*********** APP COMPONENT ***********/
var App = React.createClass({
render: function() {
return (
<NavigatorIOS initialRoute={{component: Record}} style={{flex: 1}} navigationBarHidden={true}/>
);
}
});
AppRegistry.registerComponent('Example', () => App);