capacitor-microphone 0.1.11 → 0.1.12

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,48 @@ 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;
322
+ // 1. Resolver INMEDIATAMENTE el call de restart
323
+ // Esto evita problemas de que el call quede pendiente
324
+ call.resolve(new JSObject().put("status", "restarting"));
325
+
326
+ // 2. Guardar el lenguaje
327
+ final String lang;
328
+ if (currentCall != null) {
329
+ lang = currentCall.getString("lang", "es-MX");
330
+ } else {
331
+ lang = call.getString("lang", "es-MX");
332
+ }
333
+
334
+ // 3. Detener la escucha actual si está activa
335
+ if (isListening) {
336
+ // Usar forceStop() para limpiar sin tocar currentCall
337
+ forceStop();
326
338
  }
327
339
 
328
- // 2. Guardar el lenguaje actual
329
- final String currentLang = currentCall.getString("lang", "es-MX");
340
+ // 4. Pequeño delay y comenzar de nuevo
341
+ getActivity().runOnUiThread(() -> {
342
+ getActivity().getWindow().getDecorView().postDelayed(() -> {
343
+ // Resetear flags
344
+ isListening = true;
345
+ isDestroyed = false;
346
+
347
+ // Crear NUEVO PluginCall para el inicio
348
+ PluginCall newCall = new PluginCall(
349
+ "restart_" + System.currentTimeMillis(),
350
+ "capacitor-microphone",
351
+ "startListening",
352
+ new JSObject().put("lang", lang)
353
+ );
354
+
355
+ // Iniciar nueva sesión
356
+ startListening(newCall);
357
+ }, 500); // Delay suficiente para limpieza
358
+ });
359
+ }
330
360
 
331
- // 3. Detener completamente la sesión actual
361
+ // Método helper para detener sin problemas
362
+ private void forceStop() {
363
+ // Solo cambiar flags
332
364
  isListening = false;
333
365
  isDestroyed = true;
334
366
 
@@ -343,155 +375,29 @@ private void startListeningInternal(PluginCall call, String lang) {
343
375
  delayedStartRunnable = null;
344
376
  }
345
377
 
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
378
+ // Detener speech recognizer (igual que stopSpeech pero sin tocar currentCall)
379
+ try {
380
+ lastStopTime = System.currentTimeMillis();
381
+
382
+ if (speechRecognizer != null) {
383
+ speechRecognizer.cancel();
384
+ speechRecognizer.setRecognitionListener(null);
385
+
386
+ final SpeechRecognizer recognizerToDestroy = speechRecognizer;
387
+ speechRecognizer = null;
388
+
389
+ getActivity().runOnUiThread(() -> {
380
390
  getActivity()
381
391
  .getWindow()
382
392
  .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
- }
393
+ .postDelayed(() -> {
394
+ try {
395
+ recognizerToDestroy.destroy();
396
+ } catch (Exception ignored) {}
397
+ }, 100);
398
+ });
493
399
  }
494
- });
400
+ } catch (Exception ignored) {}
495
401
  }
496
402
 
497
403
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "capacitor-microphone",
3
- "version": "0.1.11",
3
+ "version": "0.1.12",
4
4
  "description": "plugin to use the microphone",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",