react-native-rn-speech-to-text 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +97 -12
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,27 +1,111 @@
|
|
|
1
|
-
# react-native-speech-to-text
|
|
1
|
+
# react-native-rn-speech-to-text
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
react-native-rn-speech-to-text is a React Native library that enables real-time voice-to-text conversion on Android devices using the native SpeechRecognizer API. It provides simple methods to start and stop listening, along with event listeners for speech results, errors, and recognition state changes.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
|
-
|
|
8
7
|
```sh
|
|
9
|
-
npm install react-native-speech-to-text
|
|
8
|
+
npm install react-native-rn-speech-to-text
|
|
10
9
|
```
|
|
11
10
|
|
|
12
|
-
|
|
13
11
|
## Usage
|
|
14
12
|
|
|
15
|
-
|
|
16
13
|
```js
|
|
17
|
-
import {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
14
|
+
import React, { useEffect, useState } from 'react';
|
|
15
|
+
import {
|
|
16
|
+
View,
|
|
17
|
+
Button,
|
|
18
|
+
Text,
|
|
19
|
+
PermissionsAndroid,
|
|
20
|
+
Platform,
|
|
21
|
+
StyleSheet,
|
|
22
|
+
} from 'react-native';
|
|
23
|
+
|
|
24
|
+
import {
|
|
25
|
+
startListening,
|
|
26
|
+
stopListening,
|
|
27
|
+
addSpeechResultListener,
|
|
28
|
+
addSpeechErrorListener,
|
|
29
|
+
addSpeechStartListener,
|
|
30
|
+
} from 'react-native-rn-speech-to-text';
|
|
31
|
+
|
|
32
|
+
export default function App() {
|
|
33
|
+
const [text, setText] = useState < string > '';
|
|
34
|
+
const [status, setStatus] = useState < string > 'Idle';
|
|
35
|
+
|
|
36
|
+
// 🔐 Request Microphone Permission
|
|
37
|
+
const requestMicPermission = async (): Promise<boolean> => {
|
|
38
|
+
if (Platform.OS === 'android') {
|
|
39
|
+
const granted = await PermissionsAndroid.request(
|
|
40
|
+
PermissionsAndroid.PERMISSIONS.RECORD_AUDIO
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
return granted === PermissionsAndroid.RESULTS.GRANTED;
|
|
44
|
+
}
|
|
45
|
+
return false;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
const resultSub = addSpeechResultListener((speechText) => {
|
|
50
|
+
setText(speechText);
|
|
51
|
+
setStatus('Result received');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const errorSub = addSpeechErrorListener((error) => {
|
|
55
|
+
setStatus(`Error: ${error}`);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const startSub = addSpeechStartListener(() => {
|
|
59
|
+
setStatus('Listening...');
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
return () => {
|
|
63
|
+
resultSub.remove();
|
|
64
|
+
errorSub.remove();
|
|
65
|
+
startSub.remove();
|
|
66
|
+
};
|
|
67
|
+
}, []);
|
|
68
|
+
|
|
69
|
+
const handleStart = async () => {
|
|
70
|
+
const hasPermission = await requestMicPermission();
|
|
71
|
+
if (!hasPermission) {
|
|
72
|
+
setStatus('Microphone permission denied');
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
startListening();
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
return (
|
|
80
|
+
<View style={styles.container}>
|
|
81
|
+
<Text style={styles.status}>{status}</Text>
|
|
82
|
+
<Text style={styles.result}>{text}</Text>
|
|
83
|
+
|
|
84
|
+
<Button title="Start Listening" onPress={handleStart} />
|
|
85
|
+
<View style={{ height: 10 }} />
|
|
86
|
+
<Button title="Stop Listening" onPress={stopListening} />
|
|
87
|
+
</View>
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const styles = StyleSheet.create({
|
|
92
|
+
container: {
|
|
93
|
+
flex: 1,
|
|
94
|
+
justifyContent: 'center',
|
|
95
|
+
padding: 20,
|
|
96
|
+
},
|
|
97
|
+
status: {
|
|
98
|
+
fontSize: 16,
|
|
99
|
+
marginBottom: 10,
|
|
100
|
+
},
|
|
101
|
+
result: {
|
|
102
|
+
fontSize: 18,
|
|
103
|
+
fontWeight: 'bold',
|
|
104
|
+
marginBottom: 20,
|
|
105
|
+
},
|
|
106
|
+
});
|
|
22
107
|
```
|
|
23
108
|
|
|
24
|
-
|
|
25
109
|
## Contributing
|
|
26
110
|
|
|
27
111
|
- [Development workflow](CONTRIBUTING.md#development-workflow)
|
|
@@ -35,3 +119,4 @@ MIT
|
|
|
35
119
|
---
|
|
36
120
|
|
|
37
121
|
Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)
|
|
122
|
+
u
|