@reactxnative/react-native-voice 1.0.0 → 1.0.3
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 +132 -0
- package/package.json +6 -3
package/README.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# @reactxnative/react-native-voice
|
|
2
|
+
|
|
3
|
+
### 🎥 [Click here to watch the Demo Video!](https://github.com/reactxnative/react-native-voice/raw/main/demo.mov)
|
|
4
|
+
|
|
5
|
+
A high-performance, custom React Native voice recognition library built entirely using the new Android Native TurboModule Architecture (Bridgeless-ready).
|
|
6
|
+
|
|
7
|
+
It uses Android's built-in `SpeechRecognizer` API to deliver fast, offline-capable (depending on OS), and highly accurate voice-to-text recognition with no external dependencies. Currently optimized for Android.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
- 🚀 **TurboModule Architecture:** Blazing fast performance using the latest React Native architecture.
|
|
11
|
+
- 🔗 **Bridgeless Compatible:** Fully supports React Native 0.74+ and 0.86+ New Architecture defaults.
|
|
12
|
+
- 🎤 **Zero Dependencies:** Uses pure Android native `SpeechRecognizer` class.
|
|
13
|
+
- ⚡ **Auto-Unwrapping:** Super clean JS API that returns raw strings instead of bulky objects.
|
|
14
|
+
- 🇮🇳 **Multi-Lingual:** Supports free-form language modeling (default initialized for Hindi/English but highly extensible).
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
npm install @reactxnative/react-native-voice
|
|
22
|
+
```
|
|
23
|
+
or
|
|
24
|
+
```sh
|
|
25
|
+
yarn add @reactxnative/react-native-voice
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Android Permissions Setup
|
|
29
|
+
Since this library interacts with the device's hardware microphone, you must add the following permissions to your `android/app/src/main/AndroidManifest.xml`:
|
|
30
|
+
|
|
31
|
+
```xml
|
|
32
|
+
<uses-permission android:name="android.permission.RECORD_AUDIO" />
|
|
33
|
+
<uses-permission android:name="android.permission.INTERNET" />
|
|
34
|
+
```
|
|
35
|
+
*(Note: You must also request runtime permissions in your JS code using `PermissionsAndroid.request` before calling `startListening()`).*
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## Usage Example
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
import { useEffect, useState } from 'react';
|
|
43
|
+
import { Button, Text, View } from 'react-native';
|
|
44
|
+
import {
|
|
45
|
+
startListening,
|
|
46
|
+
stopListening,
|
|
47
|
+
destroy,
|
|
48
|
+
addSpeechResultsListener,
|
|
49
|
+
addSpeechErrorListener
|
|
50
|
+
} from '@reactxnative/react-native-voice';
|
|
51
|
+
|
|
52
|
+
export default function App() {
|
|
53
|
+
const [text, setText] = useState('');
|
|
54
|
+
const [error, setError] = useState<number | null>(null);
|
|
55
|
+
|
|
56
|
+
useEffect(() => {
|
|
57
|
+
// 1. Subscribe to Speech Results
|
|
58
|
+
const resultsSub = addSpeechResultsListener((spokenText: string) => {
|
|
59
|
+
console.log('User said:', spokenText);
|
|
60
|
+
setText(spokenText);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// 2. Subscribe to Speech Errors
|
|
64
|
+
const errorSub = addSpeechErrorListener((errCode: number) => {
|
|
65
|
+
console.error('Speech Error Code:', errCode);
|
|
66
|
+
setError(errCode);
|
|
67
|
+
|
|
68
|
+
// Common error codes:
|
|
69
|
+
// 6 = Timeout (No speech heard)
|
|
70
|
+
// 7 = No match (Speech not recognized)
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
return () => {
|
|
74
|
+
// Clean up listeners and destroy native recognizer when component unmounts
|
|
75
|
+
resultsSub.remove();
|
|
76
|
+
errorSub.remove();
|
|
77
|
+
destroy();
|
|
78
|
+
};
|
|
79
|
+
}, []);
|
|
80
|
+
|
|
81
|
+
return (
|
|
82
|
+
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
|
|
83
|
+
<Text style={{ fontSize: 24 }}>{text || "Press mic to speak..."}</Text>
|
|
84
|
+
|
|
85
|
+
<Button title="Start Listening" onPress={() => {
|
|
86
|
+
setError(null);
|
|
87
|
+
startListening();
|
|
88
|
+
}} />
|
|
89
|
+
|
|
90
|
+
<Button title="Stop" onPress={() => stopListening()} />
|
|
91
|
+
|
|
92
|
+
{error && <Text style={{ color: 'red' }}>Error Code: {error}</Text>}
|
|
93
|
+
</View>
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## API Reference
|
|
99
|
+
|
|
100
|
+
### Methods
|
|
101
|
+
|
|
102
|
+
| Method | Description |
|
|
103
|
+
|--------|-------------|
|
|
104
|
+
| `startListening()` | Initializes the native `SpeechRecognizer` (if not already created) and begins listening to user audio. |
|
|
105
|
+
| `stopListening()` | Instructs the recognizer to stop listening. It may still process audio already captured. |
|
|
106
|
+
| `destroy()` | Completely destroys the native recognizer instance and frees up memory. Call this when your component unmounts. |
|
|
107
|
+
|
|
108
|
+
### Event Listeners
|
|
109
|
+
|
|
110
|
+
| Method | Callback Type | Description |
|
|
111
|
+
|--------|--------------|-------------|
|
|
112
|
+
| `addSpeechResultsListener` | `(spokenText: string) => void` | Fires when the recognizer successfully determines a final result. Returns a clean string. |
|
|
113
|
+
| `addSpeechErrorListener` | `(errorCode: number) => void` | Fires when a native Android speech error occurs. |
|
|
114
|
+
|
|
115
|
+
### Common Android Speech Error Codes
|
|
116
|
+
* `1` - Network operation timed out.
|
|
117
|
+
* `2` - Other network related errors.
|
|
118
|
+
* `3` - Audio recording error.
|
|
119
|
+
* `4` - Server sends error status.
|
|
120
|
+
* `5` - Other client side errors (e.g., cancelled by user).
|
|
121
|
+
* `6` - No speech input (User was silent for 10 seconds).
|
|
122
|
+
* `7` - No recognition result matched (Couldn't understand the speech).
|
|
123
|
+
* `8` - RecognitionService busy.
|
|
124
|
+
* `9` - Insufficient permissions (You forgot to request `RECORD_AUDIO`).
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
128
|
+
## Contributing
|
|
129
|
+
Pull requests are welcome! For major changes, please open an issue first to discuss what you would like to change.
|
|
130
|
+
|
|
131
|
+
## License
|
|
132
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reactxnative/react-native-voice",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "A custom voice recognizer for React Native Android",
|
|
5
5
|
"main": "lib/commonjs/index.js",
|
|
6
6
|
"module": "lib/module/index.js",
|
|
@@ -37,10 +37,13 @@
|
|
|
37
37
|
],
|
|
38
38
|
"repository": {
|
|
39
39
|
"type": "git",
|
|
40
|
-
"url": "
|
|
40
|
+
"url": "https://github.com/reactxnative/react-native-voice"
|
|
41
41
|
},
|
|
42
|
-
"author": "Rahul Singh
|
|
42
|
+
"author": "Rahul Singh reactxnative@gmail.com",
|
|
43
43
|
"license": "MIT",
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"access": "public"
|
|
46
|
+
},
|
|
44
47
|
"peerDependencies": {
|
|
45
48
|
"react": "*",
|
|
46
49
|
"react-native": "*"
|