capacitor-microphone 0.1.14 → 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,82 +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
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
final String lang;
|
|
327
|
-
if (currentCall != null && currentCall != call) {
|
|
328
|
-
lang = currentCall.getString("lang", "es-MX");
|
|
329
|
-
} else {
|
|
330
|
-
lang = call.getString("lang", "es-MX");
|
|
324
|
+
String currentLang = "es-MX";
|
|
325
|
+
if (speechIntent != null) {
|
|
326
|
+
currentLang = speechIntent.getStringExtra(RecognizerIntent.EXTRA_LANGUAGE);
|
|
327
|
+
if (currentLang == null) currentLang = "es-MX";
|
|
331
328
|
}
|
|
329
|
+
currentLang = call.getString("lang", currentLang);
|
|
332
330
|
|
|
333
|
-
|
|
334
|
-
if (isListening) {
|
|
335
|
-
// Crear call temporal para stopListening
|
|
336
|
-
PluginCall stopCall = new PluginCall(
|
|
337
|
-
this,
|
|
338
|
-
"temp_stop_" + System.currentTimeMillis(),
|
|
339
|
-
"capacitor-microphone",
|
|
340
|
-
"stopListening",
|
|
341
|
-
new JSObject()
|
|
342
|
-
);
|
|
343
|
-
stopListening(stopCall);
|
|
344
|
-
} else {
|
|
345
|
-
// Si no estaba escuchando, limpiar manualmente
|
|
346
|
-
destroyEverything();
|
|
347
|
-
}
|
|
331
|
+
final String lang = currentLang;
|
|
348
332
|
|
|
349
|
-
//
|
|
333
|
+
// Resolver INMEDIATAMENTE
|
|
334
|
+
call.resolve(new JSObject().put("success", true));
|
|
335
|
+
|
|
336
|
+
// Hacer el restart real EN EL UI THREAD desde el inicio
|
|
350
337
|
getActivity().runOnUiThread(() -> {
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
this,
|
|
355
|
-
"restart_session_" + System.currentTimeMillis(),
|
|
356
|
-
"capacitor-microphone",
|
|
357
|
-
"startListening",
|
|
358
|
-
new JSObject().put("lang", lang)
|
|
359
|
-
);
|
|
360
|
-
|
|
361
|
-
// Iniciar nueva sesión usando tu método ya probado
|
|
362
|
-
startListening(newCall);
|
|
363
|
-
}, 600); // 600ms es suficiente
|
|
364
|
-
});
|
|
365
|
-
}
|
|
338
|
+
// DESTRUIR TODO
|
|
339
|
+
isListening = false;
|
|
340
|
+
isDestroyed = true;
|
|
366
341
|
|
|
367
|
-
|
|
368
|
-
// Limpiar runnables
|
|
369
|
-
if (delayedStartRunnable != null) {
|
|
370
|
-
getActivity().runOnUiThread(() -> {
|
|
342
|
+
if (delayedStartRunnable != null) {
|
|
371
343
|
getActivity()
|
|
372
344
|
.getWindow()
|
|
373
345
|
.getDecorView()
|
|
374
346
|
.removeCallbacks(delayedStartRunnable);
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
}
|
|
347
|
+
delayedStartRunnable = null;
|
|
348
|
+
}
|
|
378
349
|
|
|
379
|
-
|
|
380
|
-
if (speechRecognizer != null) {
|
|
381
|
-
try {
|
|
350
|
+
if (speechRecognizer != null) {
|
|
382
351
|
speechRecognizer.cancel();
|
|
352
|
+
speechRecognizer.setRecognitionListener(null);
|
|
383
353
|
speechRecognizer.destroy();
|
|
384
|
-
|
|
385
|
-
// Ignorar
|
|
354
|
+
speechRecognizer = null;
|
|
386
355
|
}
|
|
387
|
-
|
|
356
|
+
|
|
357
|
+
currentCall = null;
|
|
358
|
+
speechIntent = null;
|
|
359
|
+
|
|
360
|
+
// Reiniciar después de 500ms
|
|
361
|
+
getActivity()
|
|
362
|
+
.getWindow()
|
|
363
|
+
.getDecorView()
|
|
364
|
+
.postDelayed(() -> startListeningInternalWithoutCall(lang), 500);
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// Nueva versión que NO requiere un PluginCall
|
|
369
|
+
private void startListeningInternalWithoutCall(String lang) {
|
|
370
|
+
isDestroyed = false;
|
|
371
|
+
isListening = true;
|
|
372
|
+
|
|
373
|
+
if (getPermissionState("microphone") != PermissionState.GRANTED) {
|
|
374
|
+
return;
|
|
388
375
|
}
|
|
389
376
|
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
377
|
+
if (!SpeechRecognizer.isRecognitionAvailable(getContext())) {
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
if (speechRecognizer != null) {
|
|
382
|
+
stopSpeech();
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
getActivity().runOnUiThread(() -> {
|
|
386
|
+
try {
|
|
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);
|
|
459
|
+
} catch (Exception e) {
|
|
460
|
+
// Log error si es necesario
|
|
461
|
+
}
|
|
462
|
+
});
|
|
396
463
|
}
|
|
397
464
|
|
|
398
465
|
|