capacitor-microphone 0.1.11 → 0.1.13

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.
@@ -319,16 +319,49 @@ private void startListeningInternal(PluginCall call, String lang) {
319
319
 
320
320
  @PluginMethod
321
321
  public void restartListening(PluginCall call) {
322
- // 1. Verificar si hay sesión activa
323
- if (!isListening || currentCall == null) {
324
- call.reject("No active listening session to restart");
325
- return;
326
- }
322
+ // Resolver inmediatamente
323
+ call.resolve();
324
+
325
+ // Guardar parámetros
326
+ final String lang = call.getString("lang", "es-MX");
327
+
328
+ // Usar un enfoque diferente: llamar a los métodos internos directamente
329
+ getActivity().runOnUiThread(() -> {
330
+ // 1. Detener manualmente
331
+ if (isListening) {
332
+ isListening = false;
333
+ isDestroyed = true;
334
+
335
+ if (delayedStartRunnable != null) {
336
+ getActivity()
337
+ .getWindow()
338
+ .getDecorView()
339
+ .removeCallbacks(delayedStartRunnable);
340
+ delayedStartRunnable = null;
341
+ }
327
342
 
328
- // 2. Guardar el lenguaje actual
329
- final String currentLang = currentCall.getString("lang", "es-MX");
343
+ if (speechRecognizer != null) {
344
+ speechRecognizer.cancel();
345
+ speechRecognizer.destroy();
346
+ speechRecognizer = null;
347
+ }
348
+ }
349
+
350
+ // 2. Esperar y reiniciar
351
+ getActivity().getWindow().getDecorView().postDelayed(() -> {
352
+ // Resetear
353
+ isListening = true;
354
+ isDestroyed = false;
330
355
 
331
- // 3. Detener completamente la sesión actual
356
+ // Iniciar directamente sin crear PluginCall
357
+ startListeningInternal(call, lang);
358
+ }, 500);
359
+ });
360
+ }
361
+
362
+ // Método helper para detener sin problemas
363
+ private void forceStop() {
364
+ // Solo cambiar flags
332
365
  isListening = false;
333
366
  isDestroyed = true;
334
367
 
@@ -343,155 +376,29 @@ private void startListeningInternal(PluginCall call, String lang) {
343
376
  delayedStartRunnable = null;
344
377
  }
345
378
 
346
- // 4. Destruir el recognizer actual inmediatamente
347
- getActivity().runOnUiThread(new Runnable() {
348
- @Override
349
- public void run() {
350
- try {
351
- if (speechRecognizer != null) {
352
- speechRecognizer.cancel();
353
- speechRecognizer.setRecognitionListener(null);
354
- speechRecognizer.destroy();
355
- speechRecognizer = null;
356
- }
357
-
358
- // 5. Resetear todo el estado
359
- speechIntent = null;
360
- lastStopTime = System.currentTimeMillis();
361
-
362
- // 6. Crear NUEVO intent para evitar acumulación
363
- final Intent newIntent = new Intent(
364
- RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
365
- newIntent.putExtra(
366
- RecognizerIntent.EXTRA_LANGUAGE_MODEL,
367
- RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
368
- newIntent.putExtra(
369
- RecognizerIntent.EXTRA_LANGUAGE, currentLang);
370
- newIntent.putExtra(
371
- RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
372
-
373
- // IMPORTANTE: Agregar esta línea para NO acumular resultados
374
- newIntent.putExtra(
375
- RecognizerIntent.EXTRA_MAX_RESULTS, 1);
376
- newIntent.putExtra(
377
- RecognizerIntent.EXTRA_PREFER_OFFLINE, false);
378
-
379
- // 7. Reiniciar con nueva sesión limpia
379
+ // Detener speech recognizer (igual que stopSpeech pero sin tocar currentCall)
380
+ try {
381
+ lastStopTime = System.currentTimeMillis();
382
+
383
+ if (speechRecognizer != null) {
384
+ speechRecognizer.cancel();
385
+ speechRecognizer.setRecognitionListener(null);
386
+
387
+ final SpeechRecognizer recognizerToDestroy = speechRecognizer;
388
+ speechRecognizer = null;
389
+
390
+ getActivity().runOnUiThread(() -> {
380
391
  getActivity()
381
392
  .getWindow()
382
393
  .getDecorView()
383
- .postDelayed(new Runnable() {
384
- @Override
385
- public void run() {
386
- // Resetear flags
387
- isDestroyed = false;
388
- isListening = true;
389
-
390
- // Iniciar nueva sesión limpia
391
- startFreshSession(currentCall, currentLang, newIntent);
392
- call.resolve();
393
- }
394
- }, RESTART_COOLDOWN_MS);
395
-
396
- } catch (Exception e) {
397
- call.reject("Failed to restart: " + e.getMessage());
398
- }
399
- }
400
- });
401
- }
402
-
403
- // Método auxiliar para iniciar sesión completamente nueva
404
- private void startFreshSession(PluginCall call, String lang, Intent intent) {
405
- if (isDestroyed || !isListening) return;
406
-
407
- getActivity().runOnUiThread(new Runnable() {
408
- @Override
409
- public void run() {
410
- try {
411
- // Crear NUEVO speech recognizer
412
- speechRecognizer = SpeechRecognizer.createSpeechRecognizer(getActivity());
413
- speechRecognizer.setRecognitionListener(new RecognitionListener() {
414
- // ... (el mismo listener que ya tienes)
415
- // Copia exactamente tu listener actual aquí
416
-
417
- @Override public void onReadyForSpeech(Bundle params) {}
418
- @Override public void onBeginningOfSpeech() {}
419
- @Override public void onRmsChanged(float rmsdB) {}
420
- @Override public void onBufferReceived(byte[] buffer) {}
421
- @Override public void onEndOfSpeech() {}
422
-
423
- @Override
424
- public void onError(int error) {
425
- if (isDestroyed || !isListening) return;
426
-
427
- if (error == SpeechRecognizer.ERROR_NO_MATCH ||
428
- error == SpeechRecognizer.ERROR_SPEECH_TIMEOUT) {
429
-
430
- if (!isDestroyed && isListening && speechRecognizer != null) {
431
- speechRecognizer.startListening(intent);
432
- }
433
- return;
434
- }
435
-
436
- stopSpeech();
437
- }
438
-
439
- @Override
440
- public void onResults(Bundle results) {
441
- if (!isListening) return;
442
-
443
- ArrayList<String> matches =
444
- results.getStringArrayList(
445
- SpeechRecognizer.RESULTS_RECOGNITION);
446
-
447
- if (matches != null && !matches.isEmpty()) {
448
- // Enviar solo el texto NUEVO
449
- notifyListeners(
450
- "partialResult",
451
- new JSObject()
452
- .put("text", matches.get(0))
453
- .put("isFinal", true)
454
- );
455
- }
456
-
457
- if (!isDestroyed && isListening && speechRecognizer != null) {
458
- speechRecognizer.startListening(intent);
459
- }
460
- }
461
-
462
- @Override
463
- public void onPartialResults(Bundle partialResults) {
464
- if (!isListening || isDestroyed) return;
465
-
466
- ArrayList<String> matches =
467
- partialResults.getStringArrayList(
468
- SpeechRecognizer.RESULTS_RECOGNITION);
469
-
470
- if (matches != null && !matches.isEmpty()) {
471
- // Enviar solo texto nuevo
472
- notifyListeners(
473
- "partialResult",
474
- new JSObject()
475
- .put("text", matches.get(0))
476
- .put("isFinal", false)
477
- );
478
- }
479
- }
480
-
481
- @Override public void onEvent(int eventType, Bundle params) {}
482
- });
483
-
484
- // Iniciar con el NUEVO intent
485
- speechIntent = intent;
486
- speechRecognizer.startListening(intent);
487
-
488
- } catch (Exception e) {
489
- if (call != null) {
490
- call.reject(e.getMessage());
491
- }
492
- }
394
+ .postDelayed(() -> {
395
+ try {
396
+ recognizerToDestroy.destroy();
397
+ } catch (Exception ignored) {}
398
+ }, 100);
399
+ });
493
400
  }
494
- });
401
+ } catch (Exception ignored) {}
495
402
  }
496
403
 
497
404
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "capacitor-microphone",
3
- "version": "0.1.11",
3
+ "version": "0.1.13",
4
4
  "description": "plugin to use the microphone",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",