react-native-voice-ts 1.0.4 → 1.0.6
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/dist/hooks/useVoiceRecognition.d.ts +20 -0
- package/dist/hooks/useVoiceRecognition.js +37 -20
- package/package.json +2 -1
- package/react-native-voice.podspec +1 -1
- package/src/NativeVoiceAndroid.ts +28 -0
- package/src/NativeVoiceIOS.ts +24 -0
- package/src/VoiceModuleTypes.ts +64 -0
- package/src/VoiceUtilTypes.ts +46 -0
- package/src/components/MicIcon.tsx +208 -0
- package/src/components/VoiceMicrophone.tsx +356 -0
- package/src/components/index.ts +11 -0
- package/src/hooks/index.ts +5 -0
- package/src/hooks/useVoiceRecognition.ts +384 -0
- package/src/index.ts +523 -0
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
import React, { useEffect, useState, useCallback } from 'react';
|
|
2
|
+
import Voice from '../index';
|
|
3
|
+
import type { SpeechErrorEvent, SpeechResultsEvent } from '../VoiceModuleTypes';
|
|
4
|
+
|
|
5
|
+
export interface VoiceMicrophoneProps {
|
|
6
|
+
/**
|
|
7
|
+
* Callback fired when speech is recognized and converted to text
|
|
8
|
+
*/
|
|
9
|
+
onSpeechResult?: (text: string) => void;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Callback fired when partial results are available (real-time)
|
|
13
|
+
*/
|
|
14
|
+
onPartialResult?: (text: string) => void;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Callback fired when recording starts
|
|
18
|
+
*/
|
|
19
|
+
onStart?: () => void;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Callback fired when recording stops
|
|
23
|
+
*/
|
|
24
|
+
onStop?: () => void;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Callback fired when an error occurs
|
|
28
|
+
*/
|
|
29
|
+
onError?: (error: string) => void;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Language locale for speech recognition
|
|
33
|
+
* @default 'en-US'
|
|
34
|
+
*/
|
|
35
|
+
locale?: string;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Whether to automatically start recording on mount
|
|
39
|
+
* @default false
|
|
40
|
+
*/
|
|
41
|
+
autoStart?: boolean;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Whether to enable partial results (real-time transcription)
|
|
45
|
+
* @default true
|
|
46
|
+
*/
|
|
47
|
+
enablePartialResults?: boolean;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Whether to continue listening after getting results (continuous mode)
|
|
51
|
+
* When enabled, the microphone will automatically restart after getting results
|
|
52
|
+
* @default false
|
|
53
|
+
*/
|
|
54
|
+
continuous?: boolean;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Maximum silence duration in milliseconds before stopping (continuous mode)
|
|
58
|
+
* Only applies when continuous mode is enabled
|
|
59
|
+
* @default 5000 (5 seconds)
|
|
60
|
+
*/
|
|
61
|
+
maxSilenceDuration?: number;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Custom render function for the component
|
|
65
|
+
* Receives isRecording state and control functions
|
|
66
|
+
*/
|
|
67
|
+
children?: (props: {
|
|
68
|
+
isRecording: boolean;
|
|
69
|
+
recognizedText: string;
|
|
70
|
+
partialText: string;
|
|
71
|
+
start: () => Promise<void>;
|
|
72
|
+
stop: () => Promise<void>;
|
|
73
|
+
cancel: () => Promise<void>;
|
|
74
|
+
error: string | null;
|
|
75
|
+
}) => React.ReactNode;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* VoiceMicrophone Component
|
|
80
|
+
*
|
|
81
|
+
* A ready-to-use voice recognition component that handles microphone access,
|
|
82
|
+
* speech recognition, and provides real-time text results.
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```tsx
|
|
86
|
+
* // Simple usage with callback
|
|
87
|
+
* <VoiceMicrophone
|
|
88
|
+
* onSpeechResult={(text) => setSearchQuery(text)}
|
|
89
|
+
* />
|
|
90
|
+
*
|
|
91
|
+
* // Custom render with full control
|
|
92
|
+
* <VoiceMicrophone locale="en-US">
|
|
93
|
+
* {({ isRecording, recognizedText, start, stop }) => (
|
|
94
|
+
* <View>
|
|
95
|
+
* <Text>{recognizedText}</Text>
|
|
96
|
+
* <Button
|
|
97
|
+
* onPress={isRecording ? stop : start}
|
|
98
|
+
* title={isRecording ? 'Stop' : 'Start'}
|
|
99
|
+
* />
|
|
100
|
+
* </View>
|
|
101
|
+
* )}
|
|
102
|
+
* </VoiceMicrophone>
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
const VoiceMicrophone: React.FC<VoiceMicrophoneProps> = ({
|
|
106
|
+
onSpeechResult,
|
|
107
|
+
onPartialResult,
|
|
108
|
+
onStart,
|
|
109
|
+
onStop,
|
|
110
|
+
onError,
|
|
111
|
+
locale = 'en-US',
|
|
112
|
+
autoStart = false,
|
|
113
|
+
enablePartialResults = true,
|
|
114
|
+
continuous = false,
|
|
115
|
+
maxSilenceDuration = 5000,
|
|
116
|
+
children,
|
|
117
|
+
}) => {
|
|
118
|
+
const [isRecording, setIsRecording] = useState(false);
|
|
119
|
+
const [recognizedText, setRecognizedText] = useState('');
|
|
120
|
+
const [partialText, setPartialText] = useState('');
|
|
121
|
+
const [error, setError] = useState<string | null>(null);
|
|
122
|
+
const shouldContinueRef = React.useRef(false);
|
|
123
|
+
const silenceTimerRef = React.useRef<NodeJS.Timeout | null>(null);
|
|
124
|
+
|
|
125
|
+
useEffect(() => {
|
|
126
|
+
// Clear any existing timers on cleanup
|
|
127
|
+
return () => {
|
|
128
|
+
if (silenceTimerRef.current) {
|
|
129
|
+
clearTimeout(silenceTimerRef.current);
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
}, []);
|
|
133
|
+
|
|
134
|
+
useEffect(() => {
|
|
135
|
+
// Set up event listeners
|
|
136
|
+
Voice.onSpeechStart = () => {
|
|
137
|
+
setIsRecording(true);
|
|
138
|
+
setError(null);
|
|
139
|
+
if (silenceTimerRef.current) {
|
|
140
|
+
clearTimeout(silenceTimerRef.current);
|
|
141
|
+
}
|
|
142
|
+
onStart?.();
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
Voice.onSpeechEnd = async () => {
|
|
146
|
+
setIsRecording(false);
|
|
147
|
+
|
|
148
|
+
// In continuous mode, restart listening after results
|
|
149
|
+
if (continuous && shouldContinueRef.current) {
|
|
150
|
+
// Small delay before restarting
|
|
151
|
+
setTimeout(async () => {
|
|
152
|
+
if (shouldContinueRef.current) {
|
|
153
|
+
try {
|
|
154
|
+
await Voice.start(locale, {
|
|
155
|
+
EXTRA_PARTIAL_RESULTS: enablePartialResults,
|
|
156
|
+
});
|
|
157
|
+
} catch (err) {
|
|
158
|
+
console.error('Failed to restart voice recognition:', err);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}, 100);
|
|
162
|
+
} else {
|
|
163
|
+
onStop?.();
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
Voice.onSpeechError = (e: SpeechErrorEvent) => {
|
|
168
|
+
const errorMessage = e.error?.message || 'Unknown error';
|
|
169
|
+
setError(errorMessage);
|
|
170
|
+
setIsRecording(false);
|
|
171
|
+
shouldContinueRef.current = false;
|
|
172
|
+
if (silenceTimerRef.current) {
|
|
173
|
+
clearTimeout(silenceTimerRef.current);
|
|
174
|
+
}
|
|
175
|
+
onError?.(errorMessage);
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
Voice.onSpeechResults = (e: SpeechResultsEvent) => {
|
|
179
|
+
if (e.value && e.value.length > 0) {
|
|
180
|
+
const text = e.value[0];
|
|
181
|
+
|
|
182
|
+
// In continuous mode, append new text to existing
|
|
183
|
+
if (continuous && recognizedText) {
|
|
184
|
+
const updatedText = recognizedText + ' ' + text;
|
|
185
|
+
setRecognizedText(updatedText);
|
|
186
|
+
onSpeechResult?.(updatedText);
|
|
187
|
+
} else {
|
|
188
|
+
setRecognizedText(text);
|
|
189
|
+
onSpeechResult?.(text);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
setPartialText('');
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
if (enablePartialResults) {
|
|
197
|
+
Voice.onSpeechPartialResults = (e: SpeechResultsEvent) => {
|
|
198
|
+
if (e.value && e.value.length > 0) {
|
|
199
|
+
const text = e.value[0];
|
|
200
|
+
setPartialText(text);
|
|
201
|
+
onPartialResult?.(text);
|
|
202
|
+
|
|
203
|
+
// Reset silence timer on partial results (user is speaking)
|
|
204
|
+
if (continuous && silenceTimerRef.current) {
|
|
205
|
+
clearTimeout(silenceTimerRef.current);
|
|
206
|
+
silenceTimerRef.current = setTimeout(async () => {
|
|
207
|
+
if (shouldContinueRef.current) {
|
|
208
|
+
shouldContinueRef.current = false;
|
|
209
|
+
if (silenceTimerRef.current) {
|
|
210
|
+
clearTimeout(silenceTimerRef.current);
|
|
211
|
+
silenceTimerRef.current = null;
|
|
212
|
+
}
|
|
213
|
+
await Voice.stop();
|
|
214
|
+
onStop?.();
|
|
215
|
+
}
|
|
216
|
+
}, maxSilenceDuration);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// Cleanup
|
|
223
|
+
return () => {
|
|
224
|
+
Voice.destroy().then(Voice.removeAllListeners);
|
|
225
|
+
};
|
|
226
|
+
}, [
|
|
227
|
+
onSpeechResult,
|
|
228
|
+
onPartialResult,
|
|
229
|
+
onStart,
|
|
230
|
+
onStop,
|
|
231
|
+
onError,
|
|
232
|
+
enablePartialResults,
|
|
233
|
+
continuous,
|
|
234
|
+
recognizedText,
|
|
235
|
+
locale,
|
|
236
|
+
maxSilenceDuration,
|
|
237
|
+
]);
|
|
238
|
+
|
|
239
|
+
// Auto-start if enabled
|
|
240
|
+
useEffect(() => {
|
|
241
|
+
if (autoStart) {
|
|
242
|
+
start();
|
|
243
|
+
}
|
|
244
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
245
|
+
}, [autoStart]);
|
|
246
|
+
|
|
247
|
+
const start = useCallback(async () => {
|
|
248
|
+
try {
|
|
249
|
+
setError(null);
|
|
250
|
+
if (!continuous) {
|
|
251
|
+
setRecognizedText('');
|
|
252
|
+
setPartialText('');
|
|
253
|
+
}
|
|
254
|
+
shouldContinueRef.current = true;
|
|
255
|
+
|
|
256
|
+
// Check permission (Android only)
|
|
257
|
+
const hasPermission = await Voice.checkMicrophonePermission();
|
|
258
|
+
if (!hasPermission) {
|
|
259
|
+
const granted = await Voice.requestMicrophonePermission();
|
|
260
|
+
if (!granted) {
|
|
261
|
+
setError('Microphone permission denied');
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
await Voice.start(locale, {
|
|
267
|
+
EXTRA_PARTIAL_RESULTS: enablePartialResults,
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
// Start silence timer if in continuous mode
|
|
271
|
+
if (continuous) {
|
|
272
|
+
silenceTimerRef.current = setTimeout(async () => {
|
|
273
|
+
if (shouldContinueRef.current) {
|
|
274
|
+
shouldContinueRef.current = false;
|
|
275
|
+
if (silenceTimerRef.current) {
|
|
276
|
+
clearTimeout(silenceTimerRef.current);
|
|
277
|
+
silenceTimerRef.current = null;
|
|
278
|
+
}
|
|
279
|
+
await Voice.stop();
|
|
280
|
+
onStop?.();
|
|
281
|
+
}
|
|
282
|
+
}, maxSilenceDuration);
|
|
283
|
+
}
|
|
284
|
+
} catch (e) {
|
|
285
|
+
const errorMessage =
|
|
286
|
+
e instanceof Error ? e.message : 'Failed to start recording';
|
|
287
|
+
setError(errorMessage);
|
|
288
|
+
shouldContinueRef.current = false;
|
|
289
|
+
onError?.(errorMessage);
|
|
290
|
+
}
|
|
291
|
+
}, [
|
|
292
|
+
locale,
|
|
293
|
+
enablePartialResults,
|
|
294
|
+
onError,
|
|
295
|
+
continuous,
|
|
296
|
+
maxSilenceDuration,
|
|
297
|
+
onStop,
|
|
298
|
+
]);
|
|
299
|
+
|
|
300
|
+
const stop = useCallback(async () => {
|
|
301
|
+
try {
|
|
302
|
+
shouldContinueRef.current = false;
|
|
303
|
+
if (silenceTimerRef.current) {
|
|
304
|
+
clearTimeout(silenceTimerRef.current);
|
|
305
|
+
silenceTimerRef.current = null;
|
|
306
|
+
}
|
|
307
|
+
await Voice.stop();
|
|
308
|
+
onStop?.();
|
|
309
|
+
} catch (e) {
|
|
310
|
+
const errorMessage =
|
|
311
|
+
e instanceof Error ? e.message : 'Failed to stop recording';
|
|
312
|
+
setError(errorMessage);
|
|
313
|
+
onError?.(errorMessage);
|
|
314
|
+
}
|
|
315
|
+
}, [onError, onStop]);
|
|
316
|
+
|
|
317
|
+
const cancel = useCallback(async () => {
|
|
318
|
+
try {
|
|
319
|
+
shouldContinueRef.current = false;
|
|
320
|
+
if (silenceTimerRef.current) {
|
|
321
|
+
clearTimeout(silenceTimerRef.current);
|
|
322
|
+
silenceTimerRef.current = null;
|
|
323
|
+
}
|
|
324
|
+
await Voice.cancel();
|
|
325
|
+
setRecognizedText('');
|
|
326
|
+
setPartialText('');
|
|
327
|
+
} catch (e) {
|
|
328
|
+
const errorMessage =
|
|
329
|
+
e instanceof Error ? e.message : 'Failed to cancel recording';
|
|
330
|
+
setError(errorMessage);
|
|
331
|
+
onError?.(errorMessage);
|
|
332
|
+
}
|
|
333
|
+
}, [onError]);
|
|
334
|
+
|
|
335
|
+
// If children render prop is provided, use it
|
|
336
|
+
if (children) {
|
|
337
|
+
return (
|
|
338
|
+
<>
|
|
339
|
+
{children({
|
|
340
|
+
isRecording,
|
|
341
|
+
recognizedText,
|
|
342
|
+
partialText,
|
|
343
|
+
start,
|
|
344
|
+
stop,
|
|
345
|
+
cancel,
|
|
346
|
+
error,
|
|
347
|
+
})}
|
|
348
|
+
</>
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// Default: render nothing (headless component)
|
|
353
|
+
return null;
|
|
354
|
+
};
|
|
355
|
+
|
|
356
|
+
export default VoiceMicrophone;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { default as VoiceMicrophone } from './VoiceMicrophone';
|
|
2
|
+
export type { VoiceMicrophoneProps } from './VoiceMicrophone';
|
|
3
|
+
export {
|
|
4
|
+
MicIcon,
|
|
5
|
+
MicOffIcon,
|
|
6
|
+
MicIconFilled,
|
|
7
|
+
MicOffIconFilled,
|
|
8
|
+
MicIconWave,
|
|
9
|
+
MicOffIconWave,
|
|
10
|
+
} from './MicIcon';
|
|
11
|
+
export type { MicIconProps, MicOffIconProps } from './MicIcon';
|