capacitor-microphone 0.0.28 → 0.0.29

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.
@@ -119,7 +119,7 @@ public class CapacitorMicrophonePlugin extends Plugin implements RecognitionList
119
119
 
120
120
  delayedStartRunnable = () -> {
121
121
  if (!isDestroyed) {
122
- startListeningInternal(call, lang);
122
+ getActivity().runOnUiThread(() -> startListeningInternal(call, lang));
123
123
  } else {
124
124
  call.reject("Listening was cancelled");
125
125
  }
@@ -129,10 +129,15 @@ public class CapacitorMicrophonePlugin extends Plugin implements RecognitionList
129
129
  return;
130
130
  }
131
131
 
132
- startListeningInternal(call, lang);
132
+ getActivity().runOnUiThread(() -> startListeningInternal(call, lang));
133
133
  }
134
134
 
135
135
  private void startListeningInternal(PluginCall call, String lang) {
136
+ if (getActivity() == null) {
137
+ call.reject("Activity is null");
138
+ return;
139
+ }
140
+
136
141
  isDestroyed = false;
137
142
  isListening = true;
138
143
 
@@ -143,12 +148,12 @@ public class CapacitorMicrophonePlugin extends Plugin implements RecognitionList
143
148
 
144
149
  stopSpeech();
145
150
 
146
- if (!SpeechRecognizer.isRecognitionAvailable(getContext())) {
151
+ if (!SpeechRecognizer.isRecognitionAvailable(getActivity())) {
147
152
  call.reject("Speech recognizer not available");
148
153
  return;
149
154
  }
150
155
 
151
- speechRecognizer = SpeechRecognizer.createSpeechRecognizer(getContext());
156
+ speechRecognizer = SpeechRecognizer.createSpeechRecognizer(getActivity());
152
157
  speechRecognizer.setRecognitionListener(this);
153
158
 
154
159
  recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
@@ -156,6 +161,14 @@ public class CapacitorMicrophonePlugin extends Plugin implements RecognitionList
156
161
  RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
157
162
  recognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
158
163
  recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, lang);
164
+ recognizerIntent.putExtra(
165
+ RecognizerIntent.EXTRA_CALLING_PACKAGE,
166
+ getActivity().getPackageName()
167
+ );
168
+ recognizerIntent.putExtra(
169
+ RecognizerIntent.EXTRA_PREFER_OFFLINE,
170
+ true
171
+ );
159
172
 
160
173
  speechRecognizer.startListening(recognizerIntent);
161
174
 
@@ -188,10 +201,9 @@ public class CapacitorMicrophonePlugin extends Plugin implements RecognitionList
188
201
  handler.postDelayed(() -> {
189
202
  isDestroyed = false;
190
203
  isListening = true;
191
- startListeningInternal(call, lang);
204
+ getActivity().runOnUiThread(() -> startListeningInternal(call, lang));
192
205
  }, 250);
193
206
 
194
- call.resolve();
195
207
  }
196
208
 
197
209
  private void stopSpeech() {
@@ -210,8 +222,10 @@ public class CapacitorMicrophonePlugin extends Plugin implements RecognitionList
210
222
  stopSpeech();
211
223
 
212
224
  handler.postDelayed(() -> {
225
+ if (getActivity() == null) return;
226
+
213
227
  if (isListening && !isDestroyed && recognizerIntent != null) {
214
- speechRecognizer = SpeechRecognizer.createSpeechRecognizer(getContext());
228
+ speechRecognizer = SpeechRecognizer.createSpeechRecognizer(getActivity());
215
229
  speechRecognizer.setRecognitionListener(this);
216
230
  speechRecognizer.startListening(recognizerIntent);
217
231
  }
@@ -250,8 +264,14 @@ public class CapacitorMicrophonePlugin extends Plugin implements RecognitionList
250
264
  }
251
265
 
252
266
  @Override public void onError(int error) {
267
+
253
268
  if (!isListening || isDestroyed) return;
254
269
 
270
+ if (error == SpeechRecognizer.ERROR_RECOGNIZER_BUSY) {
271
+ handler.postDelayed(this::restartRecognition, 300);
272
+ return;
273
+ }
274
+
255
275
  // Retry on timeout / no match
256
276
  if (error == SpeechRecognizer.ERROR_NO_MATCH ||
257
277
  error == SpeechRecognizer.ERROR_SPEECH_TIMEOUT) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "capacitor-microphone",
3
- "version": "0.0.28",
3
+ "version": "0.0.29",
4
4
  "description": "plugin to use the microphone",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",