-
Notifications
You must be signed in to change notification settings - Fork 0
/
VoiceCog.tsx
158 lines (142 loc) · 4.49 KB
/
VoiceCog.tsx
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
import {Button, View} from "native-base";
import {StyleSheet, Text} from "react-native";
import React, {useState} from "react";
// @ts-ignore
import Voice from "react-native-voice"
export const VoiceCog = () => {
const [started, setStarted] = useState("");
const [recognised, setRecognised] = useState("");
const [pitch, setPitch] = useState("");
const [error, setError] = useState("");
const [end, setEnd] = useState("");
const [results, setResults] = useState<string[]>([]);
const [partialResults, setPartialResults] = useState<string[]>([]);
Voice.onSpeechStart = (e: any) => {
setStarted("√");
};
Voice.onSpeechStart = (e: any) => {
setStarted("√");
};
Voice.onSpeechEnd = (e: any) => {
setEnd("√");
};
Voice.onSpeechError = (e: { error: any; }) => {
setError(JSON.stringify(e.error));
};
Voice.onSpeechResults = (e: { value: React.SetStateAction<string[]>; }) => {
setResults(e.value);
};
Voice.onSpeechPartialResults = (e: { value: React.SetStateAction<string[]>; }) => {
setPartialResults(e.value);
};
const startRecognizing = async () => {
setRecognised("");
setPitch("");
setError("");
setStarted("");
setResults([]);
setPartialResults([]);
setEnd("");
try {
await Voice.start('en-US');
} catch (e) {
//eslint-disable-next-line
console.error(e);
}
};
const stopRecognizing = async () => {
try {
await Voice.stop();
} catch (e) {
//eslint-disable-next-line
console.error(e);
}
};
const cancelRecognizing = async () => {
try {
await Voice.cancel();
} catch (e) {
//eslint-disable-next-line
console.error(e);
}
};
const destroyRecognizer = async () => {
try {
await Voice.destroy();
} catch (e) {
//eslint-disable-next-line
console.error(e);
}
setRecognised("");
setPitch("");
setError("");
setStarted("");
setResults([]);
setPartialResults([]);
setEnd("");
};
const styles = StyleSheet.create({
button: {
width: 50,
height: 50,
},
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
action: {
textAlign: 'center',
color: '#0000FF',
marginVertical: 5,
fontWeight: 'bold',
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
stat: {
textAlign: 'center',
color: '#B0171F',
marginBottom: 1,
},
});
return (
<View>
<Text style={styles.welcome}>Welcome to React Native Voice!</Text>
<Text style={styles.instructions}>Press the button and start speaking.</Text>
<Text style={styles.stat}>{`Started: ${started}`}</Text>
<Text style={styles.stat}>{`Recognized: ${recognised}`}</Text>
<Text style={styles.stat}>{`Pitch: ${pitch}`}</Text>
<Text style={styles.stat}>{`Error: ${error}`}</Text>
<Text style={styles.stat}>Results</Text>
{results.map((result, index) => {
return (
<Text key={`result-${index}`} style={styles.stat}>
{result}
</Text>
);
})}
<Text style={styles.stat}>Partial Results</Text>
{partialResults.map((result, index) => {
return (
<Text key={`partial-result-${index}`} style={styles.stat}>
{result}
</Text>
);
})}
<Text style={styles.stat}>{`End: ${end}`}</Text>
<Button onPress={startRecognizing}><Text>Start</Text></Button>
<Button onPress={stopRecognizing}><Text>Stop Recognizing</Text></Button>
<Button onPress={cancelRecognizing}><Text>Cancel</Text></Button>
<Button onPress={destroyRecognizer}><Text style={styles.action}>Destroy</Text></Button>
</View>
)
};