-
Notifications
You must be signed in to change notification settings - Fork 13
/
App.js
120 lines (105 loc) · 2.9 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
import React, { useEffect, useState } from 'react';
import { View, Image, Text, StyleSheet, SafeAreaView, TextInput, TouchableOpacity, ActivityIndicator } from 'react-native';
import Voice from '@react-native-community/voice';
const App = () => {
const [result, setResult] = useState('')
const [isLoading, setLoading] = useState(false)
useEffect(() => {
Voice.onSpeechStart = onSpeechStartHandler;
Voice.onSpeechEnd = onSpeechEndHandler;
Voice.onSpeechResults = onSpeechResultsHandler;
return () => {
Voice.destroy().then(Voice.removeAllListeners);
}
}, [])
const onSpeechStartHandler = (e) => {
console.log("start handler==>>>", e)
}
const onSpeechEndHandler = (e) => {
setLoading(false)
console.log("stop handler", e)
}
const onSpeechResultsHandler = (e) => {
let text = e.value[0]
setResult(text)
console.log("speech result handler", e)
}
const startRecording = async () => {
setLoading(true)
try {
await Voice.start('en-Us')
} catch (error) {
console.log("error raised", error)
}
}
const stopRecording = async () => {
try {
await Voice.stop()
} catch (error) {
console.log("error raised", error)
}
}
return (
<View style={styles.container}>
<SafeAreaView>
<Text style={styles.headingText}>Speech Recoginition</Text>
<View style={styles.textInputStyle}>
<TextInput
value={result}
placeholder="your text"
style={{ flex: 1 }}
onChangeText={text => setResult(text)}
/>
{isLoading ? <ActivityIndicator size="large" color="red" />
:
<TouchableOpacity
onPress={startRecording}
>
<Image
source={{ uri: 'https://raw.githubusercontent.com/AboutReact/sampleresource/master/microphone.png' }}
style={{ width: 25, height: 25 }}
/>
</TouchableOpacity>}
</View>
<TouchableOpacity
style={{
alignSelf: 'center',
marginTop: 24,
backgroundColor: 'red',
padding: 8,
borderRadius: 4
}}
onPress={stopRecording}
>
<Text style={{ color: 'white', fontWeight: 'bold' }}>Stop</Text>
</TouchableOpacity>
</SafeAreaView>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 24
},
headingText: {
alignSelf: 'center',
marginVertical: 26,
fontWeight: 'bold',
fontSize: 26
},
textInputStyle: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
backgroundColor: 'white',
height: 48,
borderRadius: 20,
paddingHorizontal: 16,
shadowOffset: { width: 0, height: 1 },
shadowRadius: 2,
elevation: 2,
shadowOpacity: 0.4
}
});
export default App;