capacitor-microphone 0.1.15 → 0.1.16

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.
@@ -317,98 +317,149 @@ private void startListeningInternal(PluginCall call, String lang) {
317
317
  call.resolve();
318
318
  }
319
319
 
320
+
321
+
320
322
  @PluginMethod
321
323
  public void restartListening(PluginCall call) {
322
- // 1. Obtener lenguaje actual o del parámetro
323
324
  String currentLang = "es-MX";
324
- if (currentCall != null) {
325
- currentLang = currentCall.getString("lang", "es-MX");
326
- } else {
327
- currentLang = call.getString("lang", "es-MX");
325
+ if (speechIntent != null) {
326
+ currentLang = speechIntent.getStringExtra(RecognizerIntent.EXTRA_LANGUAGE);
327
+ if (currentLang == null) currentLang = "es-MX";
328
328
  }
329
+ currentLang = call.getString("lang", currentLang);
329
330
 
330
- // 2. Guardar para usar después
331
331
  final String lang = currentLang;
332
332
 
333
- // 3. Enviar respuesta INMEDIATA
333
+ // Resolver INMEDIATAMENTE
334
334
  call.resolve(new JSObject().put("success", true));
335
335
 
336
- // 4. En el background, hacer el restart REAL
337
- new Thread(() -> {
338
- try {
339
- // 4.1 Detener completamente
340
- if (isListening) {
341
- getActivity().runOnUiThread(() -> {
342
- isListening = false;
343
- isDestroyed = true;
344
-
345
- if (delayedStartRunnable != null) {
346
- getActivity()
347
- .getWindow()
348
- .getDecorView()
349
- .removeCallbacks(delayedStartRunnable);
350
- delayedStartRunnable = null;
351
- }
336
+ // Hacer el restart real EN EL UI THREAD desde el inicio
337
+ getActivity().runOnUiThread(() -> {
338
+ // DESTRUIR TODO
339
+ isListening = false;
340
+ isDestroyed = true;
352
341
 
353
- if (speechRecognizer != null) {
354
- speechRecognizer.cancel();
355
- speechRecognizer.destroy();
356
- speechRecognizer = null;
357
- }
342
+ if (delayedStartRunnable != null) {
343
+ getActivity()
344
+ .getWindow()
345
+ .getDecorView()
346
+ .removeCallbacks(delayedStartRunnable);
347
+ delayedStartRunnable = null;
348
+ }
358
349
 
359
- currentCall = null;
360
- speechIntent = null;
361
- });
350
+ if (speechRecognizer != null) {
351
+ speechRecognizer.cancel();
352
+ speechRecognizer.setRecognitionListener(null);
353
+ speechRecognizer.destroy();
354
+ speechRecognizer = null;
355
+ }
362
356
 
363
- // 4.2 Esperar 500ms
364
- Thread.sleep(500);
365
- }
357
+ currentCall = null;
358
+ speechIntent = null;
366
359
 
367
- // 4.3 Reiniciar
368
- getActivity().runOnUiThread(() -> {
369
- isListening = true;
370
- isDestroyed = false;
360
+ // Reiniciar después de 500ms
361
+ getActivity()
362
+ .getWindow()
363
+ .getDecorView()
364
+ .postDelayed(() -> startListeningInternalWithoutCall(lang), 500);
365
+ });
366
+ }
371
367
 
372
- // Usar el call ORIGINAL pero solo para startListeningInternal
373
- // No hay problema porque ya fue resuelto
374
- startListeningInternal(call, lang);
375
- });
368
+ // Nueva versión que NO requiere un PluginCall
369
+ private void startListeningInternalWithoutCall(String lang) {
370
+ isDestroyed = false;
371
+ isListening = true;
376
372
 
377
- } catch (Exception e) {
378
- // Log error si es necesario
379
- }
380
- }).start();
381
- }
373
+ if (getPermissionState("microphone") != PermissionState.GRANTED) {
374
+ return;
375
+ }
382
376
 
383
- private void destroyEverything() {
384
- // Limpiar runnables
385
- if (delayedStartRunnable != null) {
386
- getActivity().runOnUiThread(() -> {
387
- getActivity()
388
- .getWindow()
389
- .getDecorView()
390
- .removeCallbacks(delayedStartRunnable);
391
- });
392
- delayedStartRunnable = null;
377
+ if (!SpeechRecognizer.isRecognitionAvailable(getContext())) {
378
+ return;
393
379
  }
394
380
 
395
- // Limpiar speech recognizer
396
381
  if (speechRecognizer != null) {
382
+ stopSpeech();
383
+ }
384
+
385
+ getActivity().runOnUiThread(() -> {
397
386
  try {
398
- speechRecognizer.cancel();
399
- speechRecognizer.destroy();
387
+ speechRecognizer = SpeechRecognizer.createSpeechRecognizer(getActivity());
388
+ speechRecognizer.setRecognitionListener(new RecognitionListener() {
389
+ @Override public void onReadyForSpeech(Bundle params) {}
390
+ @Override public void onBeginningOfSpeech() {}
391
+ @Override public void onRmsChanged(float rmsdB) {}
392
+ @Override public void onBufferReceived(byte[] buffer) {}
393
+ @Override public void onEndOfSpeech() {}
394
+
395
+ @Override
396
+ public void onError(int error) {
397
+ if (isDestroyed || !isListening) return;
398
+
399
+ if (error == SpeechRecognizer.ERROR_NO_MATCH ||
400
+ error == SpeechRecognizer.ERROR_SPEECH_TIMEOUT) {
401
+ if (!isDestroyed && isListening && speechRecognizer != null) {
402
+ speechRecognizer.startListening(speechIntent);
403
+ }
404
+ return;
405
+ }
406
+
407
+ stopSpeech();
408
+ }
409
+
410
+ @Override
411
+ public void onResults(Bundle results) {
412
+ if (!isListening) return;
413
+
414
+ ArrayList<String> matches =
415
+ results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
416
+
417
+ if (matches != null && !matches.isEmpty()) {
418
+ notifyListeners(
419
+ "partialResult",
420
+ new JSObject()
421
+ .put("text", matches.get(0))
422
+ .put("isFinal", true)
423
+ );
424
+ }
425
+
426
+ if (!isDestroyed && isListening && speechRecognizer != null) {
427
+ speechRecognizer.startListening(speechIntent);
428
+ }
429
+ }
430
+
431
+ @Override
432
+ public void onPartialResults(Bundle partialResults) {
433
+ if (!isListening || isDestroyed) return;
434
+
435
+ ArrayList<String> matches =
436
+ partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
437
+
438
+ if (matches != null && !matches.isEmpty()) {
439
+ notifyListeners(
440
+ "partialResult",
441
+ new JSObject()
442
+ .put("text", matches.get(0))
443
+ .put("isFinal", false)
444
+ );
445
+ }
446
+ }
447
+
448
+ @Override public void onEvent(int eventType, Bundle params) {}
449
+ });
450
+
451
+ speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
452
+ speechIntent.putExtra(
453
+ RecognizerIntent.EXTRA_LANGUAGE_MODEL,
454
+ RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
455
+ speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, lang);
456
+ speechIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
457
+
458
+ speechRecognizer.startListening(speechIntent);
400
459
  } catch (Exception e) {
401
- // Ignorar
460
+ // Log error si es necesario
402
461
  }
403
- speechRecognizer = null;
404
- }
405
-
406
- // Resetear estado
407
- currentCall = null;
408
- speechIntent = null;
409
- isListening = false;
410
- isDestroyed = true;
411
- lastStopTime = System.currentTimeMillis();
462
+ });
412
463
  }
413
464
 
414
465
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "capacitor-microphone",
3
- "version": "0.1.15",
3
+ "version": "0.1.16",
4
4
  "description": "plugin to use the microphone",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",