capacitor-microphone 0.1.9 → 0.1.11

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,33 +319,178 @@ private void startListeningInternal(PluginCall call, String lang) {
319
319
 
320
320
  @PluginMethod
321
321
  public void restartListening(PluginCall call) {
322
- if (!isListening) {
323
- call.reject("Cannot restart - not currently listening");
322
+ // 1. Verificar si hay sesión activa
323
+ if (!isListening || currentCall == null) {
324
+ call.reject("No active listening session to restart");
324
325
  return;
325
326
  }
326
327
 
327
- // Obtener el lenguaje actual
328
- String currentLang = "es-MX";
329
- if (currentCall != null) {
330
- currentLang = currentCall.getString("lang", "es-MX");
331
- }
328
+ // 2. Guardar el lenguaje actual
329
+ final String currentLang = currentCall.getString("lang", "es-MX");
330
+
331
+ // 3. Detener completamente la sesión actual
332
+ isListening = false;
333
+ isDestroyed = true;
332
334
 
333
- // Guardar referencia al call actual
334
- PluginCall existingCall = currentCall;
335
+ // Limpiar runnables pendientes
336
+ if (delayedStartRunnable != null) {
337
+ getActivity().runOnUiThread(() -> {
338
+ getActivity()
339
+ .getWindow()
340
+ .getDecorView()
341
+ .removeCallbacks(delayedStartRunnable);
342
+ });
343
+ delayedStartRunnable = null;
344
+ }
335
345
 
336
- // Detener escucha
337
- stopListening(existingCall);
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
380
+ getActivity()
381
+ .getWindow()
382
+ .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
+ }
338
402
 
339
- // Programar reinicio después del cooldown
340
- getActivity().runOnUiThread(() -> {
341
- getActivity()
342
- .getWindow()
343
- .getDecorView()
344
- .postDelayed(() -> {
345
- // Iniciar nueva sesión
346
- startListeningInternal(existingCall, currentLang);
347
- call.resolve();
348
- }, RESTART_COOLDOWN_MS);
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
+ }
493
+ }
349
494
  });
350
495
  }
351
496
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "capacitor-microphone",
3
- "version": "0.1.9",
3
+ "version": "0.1.11",
4
4
  "description": "plugin to use the microphone",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",