-
Notifications
You must be signed in to change notification settings - Fork 13
/
demo.js
131 lines (114 loc) · 2.87 KB
/
demo.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
import Voice from '@react-native-community/voice';
import React, { useEffect, useState } from 'react';
import {
Text,
ActivityIndicator,
Image,
SafeAreaView,
StyleSheet,
TextInput,
TouchableHighlight,
View
} from 'react-native';
export default function TextToSpeech() {
const [isLoading, setLoading] = useState(false)
const [result, setResult] = useState('')
useEffect(() => {
Voice.onSpeechStart = onSpeechStart;
Voice.onSpeechVolumeChanged = onSpeechVolumeChanged;
Voice.onSpeechPartialResults = onSpeechPartialResults;
Voice.onSpeechEnd = onSpeechEnd;
return () => {
Voice.destroy().then(Voice.removeAllListeners);
};
}, []);
function onSpeechStart(e) {
// console.log('onSpeechStart: ', e);
};
const onSpeechVolumeChanged = (e) => {
// console.log('onSpeechVolumeChanged: ', e);
};
function onSpeechEnd(e) {
setLoading(false)
// console.log("on speec end", isLoading)
}
function onSpeechPartialResults(e) {
console.log('onSpeechPartialResults: ', e.value[0]);
setResult(e.value[0])
};
const _startRecognizing = async () => {
setLoading(true)
try {
await Voice.start('en-Us');
} catch (e) {
console.log(e);
}
};
const _stopRecognizing = async () => {
//Stops listening for speech
try {
await Voice.stop();
} catch (e) {
console.log(e);
}
};
return (
<SafeAreaView style={{ flex: 1 }}>
<View style={styles.container}>
<Text style={styles.speechText}>Speech Recoginition</Text>
<View style={styles.textInputStyle}>
<TextInput
value={result}
style={{ flex: 1 }}
placeholder="your text"
onChangeText={text => setResult(text)}
/>
{!isLoading ? <TouchableHighlight
onPress={_startRecognizing}
style={{ marginVertical: 20 }}>
<Image
style={styles.mice}
source={{
uri:
'https://raw.githubusercontent.com/AboutReact/sampleresource/master/microphone.png',
}}
/>
</TouchableHighlight>
: <ActivityIndicator color="red" size="large" />
}
</View>
</View>
</SafeAreaView>
)
}
const styles = StyleSheet.create({
mice: {
width: 25,
height: 25,
},
container: {
flex: 1,
flexDirection: 'column',
padding: 24,
backgroundColor: '#F5FCFF',
},
textInputStyle: {
flexDirection: "row",
alignItems: 'center',
justifyContent: 'space-between',
height: 48,
backgroundColor: 'white',
borderRadius: 20,
paddingHorizontal: 16,
shadowOffset: { width: 0, height: 1 },
shadowOpacity: 0.4,
shadowRadius: 2,
elevation: 2,
},
speechText: {
marginBottom: 36,
alignSelf: 'center',
fontWeight: 'bold',
fontSize: 26
}
});