capacitor-microphone 0.1.4 → 0.1.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.
@@ -21,6 +21,7 @@ import android.os.Looper;
21
21
  import androidx.annotation.Nullable;
22
22
 
23
23
  import java.util.ArrayList;
24
+ import android.os.Bundle;
24
25
 
25
26
  @CapacitorPlugin(
26
27
  name = "CapacitorMicrophone",
@@ -45,10 +46,6 @@ public class CapacitorMicrophonePlugin extends Plugin implements RecognitionList
45
46
  private final Handler handler = new Handler(Looper.getMainLooper());
46
47
  private Runnable delayedStartRunnable;
47
48
 
48
- private String lastPartial = "";
49
- private long lastSpeechTime = 0;
50
- private Runnable silenceWatcher;
51
-
52
49
 
53
50
 
54
51
 
@@ -147,7 +144,6 @@ public class CapacitorMicrophonePlugin extends Plugin implements RecognitionList
147
144
 
148
145
  isDestroyed = false;
149
146
  isListening = true;
150
- lastPartial = "";
151
147
 
152
148
 
153
149
  if (getPermissionState("microphone") != PermissionState.GRANTED) {
@@ -174,11 +170,10 @@ public class CapacitorMicrophonePlugin extends Plugin implements RecognitionList
174
170
  RecognizerIntent.EXTRA_CALLING_PACKAGE,
175
171
  getActivity().getPackageName()
176
172
  );
177
- lastSpeechTime = System.currentTimeMillis();
178
173
  speechRecognizer.startListening(recognizerIntent);
179
174
 
180
175
  call.resolve();
181
- startSilenceWatcher();
176
+
182
177
 
183
178
  }
184
179
 
@@ -186,7 +181,6 @@ public class CapacitorMicrophonePlugin extends Plugin implements RecognitionList
186
181
  public void stopListening(PluginCall call) {
187
182
  isListening = false;
188
183
  isDestroyed = true;
189
- silenceWatcher = null;
190
184
 
191
185
 
192
186
  if (delayedStartRunnable != null) {
@@ -245,7 +239,6 @@ public class CapacitorMicrophonePlugin extends Plugin implements RecognitionList
245
239
  if (isListening && !isDestroyed && recognizerIntent != null) {
246
240
  speechRecognizer = SpeechRecognizer.createSpeechRecognizer(getActivity());
247
241
  speechRecognizer.setRecognitionListener(this);
248
- lastSpeechTime = System.currentTimeMillis();
249
242
  speechRecognizer.startListening(recognizerIntent);
250
243
  }
251
244
  }, 200);
@@ -263,7 +256,6 @@ public class CapacitorMicrophonePlugin extends Plugin implements RecognitionList
263
256
  if (!isListening || isDestroyed) return;
264
257
  if (results == null) return;
265
258
 
266
- lastSpeechTime = System.currentTimeMillis();
267
259
 
268
260
  ArrayList<String> matches =
269
261
  results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
@@ -272,7 +264,6 @@ public class CapacitorMicrophonePlugin extends Plugin implements RecognitionList
272
264
 
273
265
  String text = matches.get(0);
274
266
 
275
- lastPartial = text;
276
267
 
277
268
  JSObject data = new JSObject();
278
269
  data.put("text", text);
@@ -281,57 +272,22 @@ public class CapacitorMicrophonePlugin extends Plugin implements RecognitionList
281
272
  notifyListeners("partialResult", data);
282
273
 
283
274
  if (isFinal) {
284
- lastPartial = "";
285
- handler.postDelayed(this::restartRecognition, 100);
286
- }
287
- }
288
-
289
- private void startSilenceWatcher() {
290
-
291
- if (silenceWatcher != null) return;
275
+ handler.postDelayed(this::restartRecognition, 300);
292
276
 
293
- silenceWatcher = new Runnable() {
294
- @Override
295
- public void run() {
296
-
297
- if (!isListening || isDestroyed) {
298
- silenceWatcher = null;
299
- return;
300
- }
301
-
302
- long diff = System.currentTimeMillis() - lastSpeechTime;
303
-
304
- if (diff > 2500) {
305
- restartRecognition();
306
- }
307
-
308
- handler.postDelayed(this, 1000);
309
- }
310
- };
311
-
312
- handler.postDelayed(silenceWatcher, 1000);
277
+ }
313
278
  }
314
279
 
315
280
 
316
281
 
317
282
 
318
283
  @Override public void onError(int error) {
319
-
320
284
  if (!isListening || isDestroyed) return;
321
-
322
- if (!lastPartial.isEmpty()) {
323
- JSObject data = new JSObject();
324
- data.put("text", lastPartial);
325
- data.put("isFinal", true);
326
- notifyListeners("partialResult", data);
327
- lastPartial = "";
328
- }
329
-
330
- handler.postDelayed(this::restartRecognition, 200);
285
+ handler.postDelayed(this::restartRecognition, 300);
331
286
  }
332
287
 
333
288
 
334
289
 
290
+
335
291
  @Override
336
292
  protected void handleOnDestroy() {
337
293
  isDestroyed = true;
@@ -344,12 +300,7 @@ public class CapacitorMicrophonePlugin extends Plugin implements RecognitionList
344
300
  @Override public void onBeginningOfSpeech() {}
345
301
  @Override public void onRmsChanged(float rmsdB) {}
346
302
  @Override public void onBufferReceived(byte[] buffer) {}
347
- @Override
348
- public void onEndOfSpeech() {
349
- if (!isListening || isDestroyed) return;
350
-
351
- handler.postDelayed(this::restartRecognition, 100);
352
- }
303
+ @Override public void onEndOfSpeech() {}
353
304
  @Override public void onEvent(int eventType, @Nullable android.os.Bundle params) {}
354
305
 
355
306
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "capacitor-microphone",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "plugin to use the microphone",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",