1
0
Fork 0

patched an Android 9 SDK bug causing IllegalArgumentException when trying to destroy the SpeechRecognizer

This commit is contained in:
sspanak 2024-08-05 17:22:58 +03:00 committed by Dimo Karaivanov
parent 40b1aff3b1
commit 5705be0d80

View file

@ -108,8 +108,21 @@ public class VoiceInputOps {
private void destroy() { private void destroy() {
this.language = null; this.language = null;
if (speechRecognizer != null) {
speechRecognizer.destroy(); // We try this multiple times, because it can fail due to a bug in the Android SDK
// https://github.com/sspanak/tt9/issues/593
for (int i = 0; i < 3 && speechRecognizer != null; i++) {
try {
speechRecognizer.destroy();
} catch (IllegalArgumentException e) {
if (i < 2) {
Logger.e(getClass().getSimpleName(), "SpeechRecognizer destroy failed. " + e.getMessage() + ". Retrying...");
continue;
} else {
Logger.e(getClass().getSimpleName(), "SpeechRecognizer destroy failed. " + e.getMessage() + ". Giving up and just nulling the reference.");
}
}
speechRecognizer = null; speechRecognizer = null;
Logger.d(getClass().getSimpleName(), "SpeechRecognizer destroyed"); Logger.d(getClass().getSimpleName(), "SpeechRecognizer destroyed");
} }