react-native-voice-ts 1.0.2 → 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.
Files changed (59) hide show
  1. package/README.md +287 -77
  2. package/dist/NativeVoiceAndroid.d.ts +0 -1
  3. package/dist/NativeVoiceAndroid.js +0 -1
  4. package/dist/NativeVoiceIOS.d.ts +0 -1
  5. package/dist/NativeVoiceIOS.js +0 -1
  6. package/dist/VoiceModuleTypes.d.ts +0 -1
  7. package/dist/VoiceModuleTypes.js +0 -1
  8. package/dist/VoiceUtilTypes.d.ts +0 -1
  9. package/dist/VoiceUtilTypes.js +0 -1
  10. package/dist/components/MicIcon.d.ts +24 -5
  11. package/dist/components/MicIcon.js +71 -13
  12. package/dist/components/VoiceMicrophone.d.ts +0 -1
  13. package/dist/components/VoiceMicrophone.js +0 -1
  14. package/dist/components/index.d.ts +1 -2
  15. package/dist/components/index.js +1 -2
  16. package/dist/hooks/index.d.ts +0 -1
  17. package/dist/hooks/index.js +0 -1
  18. package/dist/hooks/useVoiceRecognition.d.ts +0 -1
  19. package/dist/hooks/useVoiceRecognition.js +0 -1
  20. package/dist/index.d.ts +1 -2
  21. package/dist/index.js +1 -2
  22. package/package.json +3 -7
  23. package/CONTRIBUTING.md +0 -293
  24. package/dist/NativeVoiceAndroid.d.ts.map +0 -1
  25. package/dist/NativeVoiceAndroid.js.map +0 -1
  26. package/dist/NativeVoiceIOS.d.ts.map +0 -1
  27. package/dist/NativeVoiceIOS.js.map +0 -1
  28. package/dist/VoiceModuleTypes.d.ts.map +0 -1
  29. package/dist/VoiceModuleTypes.js.map +0 -1
  30. package/dist/VoiceUtilTypes.d.ts.map +0 -1
  31. package/dist/VoiceUtilTypes.js.map +0 -1
  32. package/dist/components/MicIcon.d.ts.map +0 -1
  33. package/dist/components/MicIcon.js.map +0 -1
  34. package/dist/components/VoiceMicrophone.d.ts.map +0 -1
  35. package/dist/components/VoiceMicrophone.js.map +0 -1
  36. package/dist/components/index.d.ts.map +0 -1
  37. package/dist/components/index.js.map +0 -1
  38. package/dist/hooks/index.d.ts.map +0 -1
  39. package/dist/hooks/index.js.map +0 -1
  40. package/dist/hooks/useVoiceRecognition.d.ts.map +0 -1
  41. package/dist/hooks/useVoiceRecognition.js.map +0 -1
  42. package/dist/index.d.ts.map +0 -1
  43. package/dist/index.js.map +0 -1
  44. package/ios/Voice.xcodeproj/project.xcworkspace/xcuserdata/olumayowadaniel.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  45. package/ios/Voice.xcodeproj/project.xcworkspace/xcuserdata/rudie_shahinian.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  46. package/plugin/src/withVoice.ts +0 -74
  47. package/plugin/tsconfig.json +0 -10
  48. package/plugin/tsconfig.tsbuildinfo +0 -1
  49. package/src/NativeVoiceAndroid.ts +0 -28
  50. package/src/NativeVoiceIOS.ts +0 -24
  51. package/src/VoiceModuleTypes.ts +0 -64
  52. package/src/VoiceUtilTypes.ts +0 -46
  53. package/src/components/MicIcon.tsx +0 -72
  54. package/src/components/VoiceMicrophone.tsx +0 -345
  55. package/src/components/index.ts +0 -4
  56. package/src/hooks/index.ts +0 -5
  57. package/src/hooks/useVoiceRecognition.ts +0 -333
  58. package/src/images/mic.svg +0 -16
  59. package/src/index.ts +0 -515
@@ -1,333 +0,0 @@
1
- import React, { useEffect, useState, useCallback } from 'react';
2
- import Voice from '../index';
3
- import type { SpeechErrorEvent, SpeechResultsEvent } from '../VoiceModuleTypes';
4
-
5
- export interface UseVoiceRecognitionOptions {
6
- /**
7
- * Language locale for speech recognition
8
- * @default 'en-US'
9
- */
10
- locale?: string;
11
-
12
- /**
13
- * Whether to enable partial results (real-time transcription)
14
- * @default true
15
- */
16
- enablePartialResults?: boolean;
17
-
18
- /**
19
- * Whether to continue listening after getting results (continuous mode)
20
- * When enabled, the microphone will automatically restart after getting results
21
- * @default false
22
- */
23
- continuous?: boolean;
24
-
25
- /**
26
- * Maximum silence duration in milliseconds before stopping (continuous mode)
27
- * Only applies when continuous mode is enabled
28
- * @default 5000 (5 seconds)
29
- */
30
- maxSilenceDuration?: number;
31
-
32
- /**
33
- * Callback fired when speech is recognized
34
- */
35
- onResult?: (text: string) => void;
36
-
37
- /**
38
- * Callback fired when an error occurs
39
- */
40
- onError?: (error: string) => void;
41
- }
42
-
43
- export interface UseVoiceRecognitionReturn {
44
- /**
45
- * Whether voice recognition is currently active
46
- */
47
- isRecording: boolean;
48
-
49
- /**
50
- * Final recognized text results
51
- */
52
- results: string[];
53
-
54
- /**
55
- * Partial results (real-time transcription)
56
- */
57
- partialResults: string[];
58
-
59
- /**
60
- * Error message if an error occurred
61
- */
62
- error: string | null;
63
-
64
- /**
65
- * Start voice recognition
66
- */
67
- start: () => Promise<void>;
68
-
69
- /**
70
- * Stop voice recognition and get final results
71
- */
72
- stop: () => Promise<void>;
73
-
74
- /**
75
- * Cancel voice recognition without getting results
76
- */
77
- cancel: () => Promise<void>;
78
-
79
- /**
80
- * Reset all state
81
- */
82
- reset: () => void;
83
- }
84
-
85
- /**
86
- * Custom hook for voice recognition
87
- *
88
- * Provides a simple interface for speech-to-text functionality with automatic
89
- * event listener setup and cleanup.
90
- *
91
- * @example
92
- * ```tsx
93
- * const { isRecording, results, start, stop } = useVoiceRecognition({
94
- * locale: 'en-US',
95
- * onResult: (text) => setSearchQuery(text),
96
- * });
97
- *
98
- * // In your component
99
- * <Button
100
- * onPress={isRecording ? stop : start}
101
- * title={isRecording ? 'Stop' : 'Start Recording'}
102
- * />
103
- * <Text>{results[0]}</Text>
104
- * ```
105
- */
106
- export const useVoiceRecognition = (
107
- options: UseVoiceRecognitionOptions = {},
108
- ): UseVoiceRecognitionReturn => {
109
- const {
110
- locale = 'en-US',
111
- enablePartialResults = true,
112
- continuous = false,
113
- maxSilenceDuration = 5000,
114
- onResult,
115
- onError,
116
- } = options;
117
-
118
- const [isRecording, setIsRecording] = useState(false);
119
- const [results, setResults] = useState<string[]>([]);
120
- const [partialResults, setPartialResults] = useState<string[]>([]);
121
- const [error, setError] = useState<string | null>(null);
122
- const [shouldContinue, setShouldContinue] = useState(false);
123
- const silenceTimerRef = React.useRef<NodeJS.Timeout | null>(null);
124
- const accumulatedTextRef = React.useRef<string>('');
125
-
126
- useEffect(() => {
127
- // Clear any existing timers on cleanup
128
- return () => {
129
- if (silenceTimerRef.current) {
130
- clearTimeout(silenceTimerRef.current);
131
- }
132
- };
133
- }, []);
134
-
135
- useEffect(() => {
136
- // Set up event listeners
137
- Voice.onSpeechStart = () => {
138
- setIsRecording(true);
139
- setError(null);
140
- if (silenceTimerRef.current) {
141
- clearTimeout(silenceTimerRef.current);
142
- }
143
- };
144
-
145
- Voice.onSpeechEnd = async () => {
146
- setIsRecording(false);
147
-
148
- // In continuous mode, restart listening after results
149
- if (continuous && shouldContinue) {
150
- setTimeout(async () => {
151
- if (shouldContinue) {
152
- try {
153
- await Voice.start(locale, {
154
- EXTRA_PARTIAL_RESULTS: enablePartialResults,
155
- });
156
- } catch (err) {
157
- console.error('Failed to restart voice recognition:', err);
158
- }
159
- }
160
- }, 100);
161
- }
162
- };
163
-
164
- Voice.onSpeechError = (e: SpeechErrorEvent) => {
165
- const errorMessage = e.error?.message || 'Unknown error';
166
- setError(errorMessage);
167
- setIsRecording(false);
168
- setShouldContinue(false);
169
- if (silenceTimerRef.current) {
170
- clearTimeout(silenceTimerRef.current);
171
- }
172
- onError?.(errorMessage);
173
- };
174
-
175
- Voice.onSpeechResults = (e: SpeechResultsEvent) => {
176
- if (e.value && e.value.length > 0) {
177
- if (continuous) {
178
- // Append new text to accumulated text
179
- const newText = e.value[0];
180
- accumulatedTextRef.current = accumulatedTextRef.current
181
- ? accumulatedTextRef.current + ' ' + newText
182
- : newText;
183
- setResults([accumulatedTextRef.current, ...e.value.slice(1)]);
184
- onResult?.(accumulatedTextRef.current);
185
- } else {
186
- setResults(e.value);
187
- const firstResult = e.value[0];
188
- if (firstResult) {
189
- onResult?.(firstResult);
190
- }
191
- }
192
- }
193
- };
194
-
195
- if (enablePartialResults) {
196
- Voice.onSpeechPartialResults = (e: SpeechResultsEvent) => {
197
- if (e.value && e.value.length > 0) {
198
- setPartialResults(e.value);
199
-
200
- // Reset silence timer on partial results (user is speaking)
201
- if (continuous && silenceTimerRef.current) {
202
- clearTimeout(silenceTimerRef.current);
203
- silenceTimerRef.current = setTimeout(() => {
204
- if (shouldContinue) {
205
- stop();
206
- }
207
- }, maxSilenceDuration);
208
- }
209
- }
210
- };
211
- }
212
-
213
- // Cleanup
214
- return () => {
215
- Voice.destroy().then(Voice.removeAllListeners);
216
- };
217
- }, [
218
- enablePartialResults,
219
- onResult,
220
- onError,
221
- continuous,
222
- shouldContinue,
223
- locale,
224
- maxSilenceDuration,
225
- ]);
226
-
227
- const start = useCallback(async () => {
228
- try {
229
- setError(null);
230
- if (!continuous) {
231
- setResults([]);
232
- setPartialResults([]);
233
- accumulatedTextRef.current = '';
234
- }
235
- setShouldContinue(true);
236
-
237
- // Check permission (Android only)
238
- const hasPermission = await Voice.checkMicrophonePermission();
239
- if (!hasPermission) {
240
- const granted = await Voice.requestMicrophonePermission();
241
- if (!granted) {
242
- setError('Microphone permission denied');
243
- return;
244
- }
245
- }
246
-
247
- await Voice.start(locale, {
248
- EXTRA_PARTIAL_RESULTS: enablePartialResults,
249
- });
250
-
251
- // Start silence timer if in continuous mode
252
- if (continuous) {
253
- silenceTimerRef.current = setTimeout(() => {
254
- if (shouldContinue) {
255
- stop();
256
- }
257
- }, maxSilenceDuration);
258
- }
259
- } catch (e) {
260
- const errorMessage =
261
- e instanceof Error ? e.message : 'Failed to start recording';
262
- setError(errorMessage);
263
- setShouldContinue(false);
264
- onError?.(errorMessage);
265
- }
266
- }, [
267
- locale,
268
- enablePartialResults,
269
- onError,
270
- continuous,
271
- maxSilenceDuration,
272
- shouldContinue,
273
- ]);
274
-
275
- const stop = useCallback(async () => {
276
- try {
277
- setShouldContinue(false);
278
- if (silenceTimerRef.current) {
279
- clearTimeout(silenceTimerRef.current);
280
- silenceTimerRef.current = null;
281
- }
282
- await Voice.stop();
283
- } catch (e) {
284
- const errorMessage =
285
- e instanceof Error ? e.message : 'Failed to stop recording';
286
- setError(errorMessage);
287
- onError?.(errorMessage);
288
- }
289
- }, [onError]);
290
-
291
- const cancel = useCallback(async () => {
292
- try {
293
- setShouldContinue(false);
294
- if (silenceTimerRef.current) {
295
- clearTimeout(silenceTimerRef.current);
296
- silenceTimerRef.current = null;
297
- }
298
- await Voice.cancel();
299
- setResults([]);
300
- setPartialResults([]);
301
- accumulatedTextRef.current = '';
302
- } catch (e) {
303
- const errorMessage =
304
- e instanceof Error ? e.message : 'Failed to cancel recording';
305
- setError(errorMessage);
306
- onError?.(errorMessage);
307
- }
308
- }, [onError]);
309
-
310
- const reset = useCallback(() => {
311
- setResults([]);
312
- setPartialResults([]);
313
- setError(null);
314
- setIsRecording(false);
315
- accumulatedTextRef.current = '';
316
- setShouldContinue(false);
317
- if (silenceTimerRef.current) {
318
- clearTimeout(silenceTimerRef.current);
319
- silenceTimerRef.current = null;
320
- }
321
- }, []);
322
-
323
- return {
324
- isRecording,
325
- results,
326
- partialResults,
327
- error,
328
- start,
329
- stop,
330
- cancel,
331
- reset,
332
- };
333
- };
@@ -1,16 +0,0 @@
1
- <svg
2
- xmlns="http://www.w3.org/2000/svg"
3
- width="24"
4
- height="24"
5
- viewBox="0 0 24 24"
6
- fill="none"
7
- stroke="currentColor"
8
- stroke-width="2"
9
- stroke-linecap="round"
10
- stroke-linejoin="round"
11
- class="lucide lucide-mic-icon lucide-mic"
12
- >
13
- <path d="M12 19v3" />
14
- <path d="M19 10v2a7 7 0 0 1-14 0v-2" />
15
- <rect x="9" y="2" width="6" height="13" rx="3" />
16
- </svg>;