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 (
|
|
325
|
-
currentLang =
|
|
326
|
-
|
|
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
|
-
//
|
|
333
|
+
// Resolver INMEDIATAMENTE
|
|
334
334
|
call.resolve(new JSObject().put("success", true));
|
|
335
335
|
|
|
336
|
-
//
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
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
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
342
|
+
if (delayedStartRunnable != null) {
|
|
343
|
+
getActivity()
|
|
344
|
+
.getWindow()
|
|
345
|
+
.getDecorView()
|
|
346
|
+
.removeCallbacks(delayedStartRunnable);
|
|
347
|
+
delayedStartRunnable = null;
|
|
348
|
+
}
|
|
358
349
|
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
350
|
+
if (speechRecognizer != null) {
|
|
351
|
+
speechRecognizer.cancel();
|
|
352
|
+
speechRecognizer.setRecognitionListener(null);
|
|
353
|
+
speechRecognizer.destroy();
|
|
354
|
+
speechRecognizer = null;
|
|
355
|
+
}
|
|
362
356
|
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
}
|
|
357
|
+
currentCall = null;
|
|
358
|
+
speechIntent = null;
|
|
366
359
|
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
360
|
+
// Reiniciar después de 500ms
|
|
361
|
+
getActivity()
|
|
362
|
+
.getWindow()
|
|
363
|
+
.getDecorView()
|
|
364
|
+
.postDelayed(() -> startListeningInternalWithoutCall(lang), 500);
|
|
365
|
+
});
|
|
366
|
+
}
|
|
371
367
|
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
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
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
}).start();
|
|
381
|
-
}
|
|
373
|
+
if (getPermissionState("microphone") != PermissionState.GRANTED) {
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
382
376
|
|
|
383
|
-
|
|
384
|
-
|
|
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.
|
|
399
|
-
speechRecognizer.
|
|
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
|
-
//
|
|
460
|
+
// Log error si es necesario
|
|
402
461
|
}
|
|
403
|
-
|
|
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
|
|