jamsch / expo-speech-recognition

Speech Recognition for React Native Expo projects
MIT License
128 stars 11 forks source link

[Error: Failed to retrieve supported locales with error: 14] when calling ExpoSpeechRecognitionModule.getSupportedLocales #60

Open NamHyeongKeol opened 1 week ago

NamHyeongKeol commented 1 week ago

This error occurs quite frequently when using ExpoSpeechRecognitionModule.getSupportedLocales. However, it doesn’t always happen—when running the feature that calls this function multiple times, it works sometimes and fails other times. Usually, the first time the feature is executed after launching the app, the error almost always occurs. After that, it may or may not happen.

Interestingly, after experimenting further, there were cases where the error message ([Error: Failed to retrieve supported locales with error: 14]) didn’t appear at all, and the function simply didn’t run. I am using the fetchSupportedLocalesForAndroid function defined as below, but in this situation, not even a console.log is printed, nor does any error message show up. There was only one instance where this issue occurred, but restarting the phone resolved it.

Is there any way to ensure this function runs reliably?

export const fetchSupportedLocalesForAndroid = async () => {
  try {
    const supportedLocalesTTS = await ExpoSpeechRecognitionModule.getSupportedLocales({
      androidRecognitionServicePackage: "com.google.android.tts"
    });
    console.log("tts Supported locales:", supportedLocalesTTS.locales.join(", "));
    console.log("tts On-device locales:", supportedLocalesTTS.installedLocales.join(", "));

    const supportedLocalesAS = await ExpoSpeechRecognitionModule.getSupportedLocales({
      androidRecognitionServicePackage: "com.google.android.as"
    });
    console.log("as Supported locales:", supportedLocalesAS.locales.join(", "));
    console.log("as On-device locales:", supportedLocalesAS.installedLocales.join(", "));

    return { supportedLocalesTTS, supportedLocalesAS };
  } catch (error) {
    console.log('Error fetching supported locales:', error);
    return { supportedLocalesTTS: { locales: [], installedLocales: [] }, supportedLocalesAS: { locales: [], installedLocales: [] } };
  }
};
jamsch commented 1 week ago

I ran in to this error as well when developing the feature to be able to specify the android service package. The error 14 corresponds to SpeechRecognizer.ERROR_CANNOT_CHECK_SUPPORT

What happens is that I'm calling the SpeechRecognizer.checkRecognitionSupport() function with the callbacks: onSupportResult() and onError(). The issue is that with some of the speech services, both the onError() and onSupportResult() fire but I can't actually figure out why that's the case.

So I ended up adding a 50ms timeout on the onError() invocation which worked okay on my device, but that might just need to be tweaked.

https://github.com/jamsch/expo-speech-recognition/blob/eef1cd000ef11e42586681a41f60f9aac9ffdadd/android/src/main/java/expo/modules/speechrecognition/ExpoSpeechRecognitionModule.kt#L466-L484