0

I want to check whether a specific language is downloaded or not on user's device for Android TextToSpeech.

I tried to do that:

private void checkVoiceData() {
        if (ttsLang == TextToSpeech.LANG_MISSING_DATA
                || ttsLang == TextToSpeech.LANG_NOT_SUPPORTED
                || !(ttsLang == TextToSpeech.LANG_AVAILABLE)) {

            Log.e("TTS", "The Language is not supported!");
            Toast.makeText(PreviewActivity.this, "Open WI-Fi to Download the Language..", Toast.LENGTH_LONG).show();
        }
    }

but that didn't work. I think this code doesn't check that a language is downloaded or not, is there is a way to check that?

Brendan
  • 1,017
  • 5
  • 7
Ziad H.
  • 528
  • 1
  • 5
  • 20
  • Possible duplicate of [Get available locales for text to speech (TTS)](https://stackoverflow.com/questions/4872702/get-available-locales-for-text-to-speech-tts) – Taseer Sep 07 '19 at 21:28
  • No, it's not. They check if a language is supported or get all available languages. But what I want is to check if a language -like chinese for example- is downloaded on user's device or not... If there is something I understand wrong, please tell me! – Ziad H. Sep 07 '19 at 22:42
  • FYI the more recent versions of the Google TTS engine will handle all this automatically themselves -- if the language isn't downloaded yet, it will download it immediately when you call speak() (as long as the language is "available"/supported). I have a related answer here: https://stackoverflow.com/questions/53910128/how-can-i-use-japanese-google-tts-engine-in-my-android-app-without-change-any-de?rq=1 – Nerdy Bunz Sep 08 '19 at 21:37
  • Yes! but I wanted to tell the user to open their wifi if the language isn't downloaded so they know that it's not a problem with the app. – Ziad H. Sep 08 '19 at 21:45
  • 1
    Right... so I think the best way is to send the user to the TTS settings if a speak() call causes a subsequent onError() call with an alert that says something like "error speaking [specific language]." That way they will understand they need wifi on in order to download it without you saying so. – Nerdy Bunz Sep 09 '19 at 19:41

1 Answers1

0

So, I finally found a way to check if a language is installed. Here is the code:

private boolean isLanguageInstalled(){
        Voice voice = textToSpeech.getVoice();
        if(voice != null){
            Set<String> features = voice.getFeatures();

            //Simplified if statement that returns true if the condition is met
            return features != null && !features.contains(TextToSpeech.Engine.KEY_FEATURE_NOT_INSTALLED);
        }

        return false;
    }
Ziad H.
  • 528
  • 1
  • 5
  • 20