react-native-voice-ts 1.0.4 → 1.0.5

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.
@@ -25,10 +25,30 @@ export interface UseVoiceRecognitionOptions {
25
25
  * Callback fired when speech is recognized
26
26
  */
27
27
  onResult?: (text: string) => void;
28
+ /**
29
+ * Callback fired when partial results are available (real-time)
30
+ */
31
+ onPartialResult?: (text: string) => void;
28
32
  /**
29
33
  * Callback fired when an error occurs
30
34
  */
31
35
  onError?: (error: string) => void;
36
+ /**
37
+ * Callback fired when recording starts
38
+ */
39
+ onStart?: () => void;
40
+ /**
41
+ * Callback fired when recording ends
42
+ */
43
+ onEnd?: () => void;
44
+ /**
45
+ * Callback fired when speech is recognized (before results)
46
+ */
47
+ onRecognized?: () => void;
48
+ /**
49
+ * Callback fired when volume changes (0-10)
50
+ */
51
+ onVolumeChanged?: (value: number) => void;
32
52
  }
33
53
  export interface UseVoiceRecognitionReturn {
34
54
  /**
@@ -22,7 +22,7 @@ import Voice from '../index';
22
22
  * ```
23
23
  */
24
24
  export const useVoiceRecognition = (options = {}) => {
25
- const { locale = 'en-US', enablePartialResults = true, continuous = false, maxSilenceDuration = 5000, onResult, onError, } = options;
25
+ const { locale = 'en-US', enablePartialResults = true, continuous = false, maxSilenceDuration = 5000, onResult, onPartialResult, onError, onStart, onEnd, onRecognized, onVolumeChanged, } = options;
26
26
  const [isRecording, setIsRecording] = useState(false);
27
27
  const [results, setResults] = useState([]);
28
28
  const [partialResults, setPartialResults] = useState([]);
@@ -46,9 +46,11 @@ export const useVoiceRecognition = (options = {}) => {
46
46
  if (silenceTimerRef.current) {
47
47
  clearTimeout(silenceTimerRef.current);
48
48
  }
49
+ onStart?.();
49
50
  };
50
51
  Voice.onSpeechEnd = async () => {
51
52
  setIsRecording(false);
53
+ onEnd?.();
52
54
  // In continuous mode, restart listening after results
53
55
  if (continuous && shouldContinueRef.current) {
54
56
  setTimeout(async () => {
@@ -65,6 +67,9 @@ export const useVoiceRecognition = (options = {}) => {
65
67
  }, 100);
66
68
  }
67
69
  };
70
+ Voice.onSpeechRecognized = () => {
71
+ onRecognized?.();
72
+ };
68
73
  Voice.onSpeechError = (e) => {
69
74
  const errorMessage = e.error?.message || 'Unknown error';
70
75
  setError(errorMessage);
@@ -95,27 +100,34 @@ export const useVoiceRecognition = (options = {}) => {
95
100
  }
96
101
  }
97
102
  };
98
- if (enablePartialResults) {
99
- Voice.onSpeechPartialResults = (e) => {
100
- if (e.value && e.value.length > 0) {
101
- setPartialResults(e.value);
102
- // Reset silence timer on partial results (user is speaking)
103
- if (continuous && silenceTimerRef.current) {
104
- clearTimeout(silenceTimerRef.current);
105
- silenceTimerRef.current = setTimeout(async () => {
106
- if (shouldContinueRef.current) {
107
- shouldContinueRef.current = false;
108
- if (silenceTimerRef.current) {
109
- clearTimeout(silenceTimerRef.current);
110
- silenceTimerRef.current = null;
111
- }
112
- await Voice.stop();
103
+ Voice.onSpeechPartialResults = (e) => {
104
+ if (e.value && e.value.length > 0) {
105
+ setPartialResults(e.value);
106
+ const firstPartial = e.value[0];
107
+ if (firstPartial) {
108
+ onPartialResult?.(firstPartial);
109
+ }
110
+ // Reset silence timer on partial results (user is speaking)
111
+ if (continuous && silenceTimerRef.current) {
112
+ clearTimeout(silenceTimerRef.current);
113
+ silenceTimerRef.current = setTimeout(async () => {
114
+ if (shouldContinueRef.current) {
115
+ shouldContinueRef.current = false;
116
+ if (silenceTimerRef.current) {
117
+ clearTimeout(silenceTimerRef.current);
118
+ silenceTimerRef.current = null;
113
119
  }
114
- }, maxSilenceDuration);
115
- }
120
+ await Voice.stop();
121
+ }
122
+ }, maxSilenceDuration);
116
123
  }
117
- };
118
- }
124
+ }
125
+ };
126
+ Voice.onSpeechVolumeChanged = (e) => {
127
+ if (e.value !== undefined) {
128
+ onVolumeChanged?.(e.value);
129
+ }
130
+ };
119
131
  // Cleanup
120
132
  return () => {
121
133
  Voice.destroy().then(Voice.removeAllListeners);
@@ -123,7 +135,12 @@ export const useVoiceRecognition = (options = {}) => {
123
135
  }, [
124
136
  enablePartialResults,
125
137
  onResult,
138
+ onPartialResult,
126
139
  onError,
140
+ onStart,
141
+ onEnd,
142
+ onRecognized,
143
+ onVolumeChanged,
127
144
  continuous,
128
145
  locale,
129
146
  maxSilenceDuration,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "react-native-voice-ts",
3
3
  "description": "Advanced Speech-to-Text library for React Native with TypeScript support. Features ready-to-use components (VoiceMicrophone), custom hooks (useVoiceRecognition), real-time transcription, multi-language support, and comprehensive voice recognition capabilities for iOS and Android.",
4
- "version": "1.0.4",
4
+ "version": "1.0.5",
5
5
  "author": "Noor Mohammad <noor.jsdivs@gmail.com>",
6
6
  "private": false,
7
7
  "homepage": "https://github.com/noorjsdivs/react-native-voice-ts",