-
Notifications
You must be signed in to change notification settings - Fork 17
/
App.js
188 lines (169 loc) · 4.28 KB
/
App.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
import React from 'react';
import {
StyleSheet,
Text,
TextInput,
View,
CameraRoll,
Button,
NativeModules,
Platform,
DeviceEventEmitter,
Keyboard
} from 'react-native';
import VideoPlayer from 'react-native-video-controls';
const { RecorderManager } = NativeModules;
export default class App extends React.Component {
state = {
videoUri: null,
disableStart: false,
disableStopped: true,
disablePlayable: true,
androidVideoUrl: null,
keyboardIsShown: false,
}
start = () => {
RecorderManager.start();
this.setState({
disableStart: true,
disableStopped: false,
disablePlayable: true,
});
}
stop = () => {
RecorderManager.stop();
this.setState({
disableStart: true,
disableStopped: true,
disablePlayable: false,
});
}
play = () => {
switch (Platform.OS) {
case 'android':
const { androidVideoUrl } = this.state;
if (androidVideoUrl) {
this.setState({
videoUri: androidVideoUrl,
disableStart: true,
disableStopped: true,
disablePlayable: true,
})
}
break;
case 'ios':
CameraRoll.getPhotos({
first: 1,
assetType: 'Videos'
}).then(r => {
if (r.edges.length > 0) {
const video = r.edges[0].node.image;
this.setState({
videoUri: video.uri,
disableStart: true,
disableStopped: true,
disablePlayable: true,
})
}
});
break;
default:
break;
}
}
playEnded = () => {
this.setState({
videoUri: null,
disableStart: false,
disableStopped: true,
disablePlayable: true,
androidVideoUrl: null,
});
}
keyboardDidShow = () => {
this.setState({keyboardIsShown: true});
}
keyboardDidHide = () => {
this.setState({keyboardIsShown: false});
}
rendernControlBtnGroup = () => {
const { disableStart, disableStopped, disablePlayable } = this.state;
return (
<View style={styles.footer}>
<Button style={styles.button} disabled={disableStart} title="Start" onPress={this.start} />
<Button style={styles.button} disabled={disableStopped} title="Stop" onPress={this.stop} />
<Button style={styles.button} disabled={disablePlayable} title="Play" onPress={this.play} />
</View>
)
}
componentWillMount() {
DeviceEventEmitter.addListener('updateFilePath', (filePath) => {
console.log(filePath);
this.setState({androidVideoUrl: filePath});
});
}
componentDidMount () {
this.keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', this.keyboardDidShow);
this.keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', this.keyboardDidHide);
}
componentWillUnmount () {
this.keyboardDidShowListener.remove();
this.keyboardDidHideListener.remove();
}
render() {
const { videoUri, keyboardIsShown } = this.state;
return (
<View style={styles.container}>
{Platform.OS === 'ios' && keyboardIsShown &&
this.rendernControlBtnGroup()
}
<View style={styles.content}>
{videoUri &&
<VideoPlayer
source={{ uri: videoUri }}
onEnd={this.playEnded}
/>
}
{!videoUri &&
<TextInput
style={styles.textInput}
multiline
underlineColorAndroid="white"
onChangeText={(text) => this.setState({text})}
value={this.state.text}
/>
}
</View>
{this.rendernControlBtnGroup()}
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
paddingTop: 20,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
content: {
flex: 1,
flexDirection: 'row',
padding: 20,
justifyContent: 'center',
},
textInput: {
borderColor: 'gray',
borderWidth: 1,
flex: 1,
fontSize: 24
},
footer: {
backgroundColor: Platform.OS==='ios' ? '#eee' : '#fff',
flexDirection: 'row',
alignSelf: 'stretch',
justifyContent: 'center',
paddingVertical: 20,
},
});