-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
251 lines (233 loc) · 6.37 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
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
251
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow strict-local
*/
import React, {useState, useEffect} from 'react';
// import type {Node} from 'react';
import {
SafeAreaView,
StyleSheet,
Text,
View,
Image,
TouchableHighlight,
ScrollView,
} from 'react-native';
// import Voice
import Voice from 'react-native-voice';
const App = () => {
const [pitch, setPitch] = useState('');
const [error, setError] = useState('');
const [end, setEnd] = useState('');
const [started, setStarted] = useState('');
const [results, setResults] = useState([]);
const [partialResults, setPartialResults] = useState([]);
useEffect(() => {
//Setting callbacks for the process status
Voice.onSpeechStart = onSpeechStart;
Voice.onSpeechEnd = onSpeechEnd;
Voice.onSpeechError = onSpeechError;
Voice.onSpeechResults = onSpeechResults;
Voice.onSpeechPartialResults = onSpeechPartialResults;
Voice.onSpeechVolumeChanged = onSpeechVolumeChanged;
return () => {
//destroy the process after switching the screen
Voice.destroy().then(Voice.removeAllListeners);
};
}, []);
const onSpeechStart = (e) => {
//Invoked when .start() is called without error
console.log('onSpeechStart: ', e);
setStarted('√');
};
const onSpeechEnd = (e) => {
//Invoked when SpeechRecognizer stops recognition
console.log('onSpeechEnd: ', e);
setEnd('√');
};
const onSpeechError = (e) => {
//Invoked when an error occurs.
console.log('onSpeechError: ', e);
setError(JSON.stringify(e.error));
};
const onSpeechResults = (e) => {
//Invoked when SpeechRecognizer is finished recognizing
console.log('onSpeechResults: ', e);
setResults(e.value);
};
const onSpeechPartialResults = (e) => {
//Invoked when any results are computed
console.log('onSpeechPartialResults: ', e);
setPartialResults(e.value);
};
const onSpeechVolumeChanged = (e) => {
//Invoked when pitch that is recognized changed
console.log('onSpeechVolumeChanged: ', e);
setPitch(e.value);
};
const startRecognizing = async () => {
//Starts listening for speech for a specific locale
try {
await Voice.start('en-US');
setPitch('');
setError('');
setStarted('');
setResults([]);
setPartialResults([]);
setEnd('');
} catch (e) {
//eslint-disable-next-line
console.error(e);
}
};
const stopRecognizing = async () => {
//Stops listening for speech
try {
await Voice.stop();
} catch (e) {
//eslint-disable-next-line
console.error(e);
}
};
const cancelRecognizing = async () => {
//Cancels the speech recognition
try {
await Voice.cancel();
} catch (e) {
//eslint-disable-next-line
console.error(e);
}
};
const destroyRecognizer = async () => {
//Destroys the current SpeechRecognizer instance
try {
await Voice.destroy();
setPitch('');
setError('');
setStarted('');
setResults([]);
setPartialResults([]);
setEnd('');
} catch (e) {
//eslint-disable-next-line
console.error(e);
}
};
return (
<SafeAreaView style={styles.container}>
<View style={styles.container}>
<Text style={styles.titleText}>
Speech to Text
</Text>
<Text style={styles.textStyle}>Press to start recording</Text>
<View style={styles.headerContainer}>
<Text style={styles.textWithSpaceStyle}>{`Started: ${started}`}</Text>
<Text style={styles.textWithSpaceStyle}>{`End: ${end}`}</Text>
</View>
<View style={styles.headerContainer}>
<Text style={styles.textWithSpaceStyle}>{`Pitch: \n ${pitch}`}</Text>
<Text style={styles.textWithSpaceStyle}>{`Error: \n ${error}`}</Text>
</View>
<TouchableHighlight onPress={startRecognizing}>
<Image
style={styles.imageButton}
source={{
uri:
'https://raw.githubusercontent.com/AboutReact/sampleresource/master/microphone.png',
}}
/>
</TouchableHighlight>
<Text style={styles.textStyle}>Partial Results</Text>
<ScrollView>
{partialResults.map((result, index) => {
return (
<Text key={`partial-result-${index}`} style={styles.textStyle}>
{result}
</Text>
);
})}
</ScrollView>
<Text style={styles.textStyle}>Results</Text>
<ScrollView style={{marginBottom: 42}}>
{results.map((result, index) => {
return (
<Text key={`result-${index}`} style={styles.textStyle}>
{result}
</Text>
);
})}
</ScrollView>
<View style={styles.horizontalView}>
<TouchableHighlight
onPress={stopRecognizing}
style={styles.buttonStyle}>
<Text style={styles.buttonTextStyle}>Stop</Text>
</TouchableHighlight>
<TouchableHighlight
onPress={cancelRecognizing}
style={styles.buttonStyle}>
<Text style={styles.buttonTextStyle}>Cancel</Text>
</TouchableHighlight>
<TouchableHighlight
onPress={destroyRecognizer}
style={styles.buttonStyle}>
<Text style={styles.buttonTextStyle}>Clear</Text>
</TouchableHighlight>
</View>
</View>
</SafeAreaView>
);
};
export default App;
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
alignItems: 'center',
padding: 5,
},
headerContainer: {
flexDirection: 'row',
justifyContent: 'space-between',
paddingVertical: 10,
},
titleText: {
fontSize: 22,
textAlign: 'center',
fontWeight: 'bold',
},
buttonStyle: {
flex: 1,
justifyContent: 'center',
marginTop: 15,
padding: 10,
backgroundColor: '#8ad24e',
marginRight: 2,
marginLeft: 2,
},
buttonTextStyle: {
color: '#fff',
textAlign: 'center',
},
horizontalView: {
flexDirection: 'row',
position: 'absolute',
bottom: 0,
},
textStyle: {
textAlign: 'center',
padding: 12,
},
imageButton: {
width: 50,
height: 50,
},
textWithSpaceStyle: {
flex: 1,
textAlign: 'center',
color: '#B0171F',
},
});