Search code examples
androidspeech-recognitiontext-to-speech

Check if speech packages is already in android


Hello i'm building an app that uses speech recognition, my intention is, that user can use the app without internet, so for he do it he need download an language package inside his smartphone.

1 - Is possible check if user already has the package inside the phone?
2 - Can i show the speech packages system configuration to the user ( like wifi or gps configuration )?


Solution

  • To check for TTS installed . Use this code from your activity

    Intent checkTTSIntent = new Intent();
            checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
    
            checkTTSIntent.putExtra(
                    TextToSpeech.Engine.EXTRA_CHECK_VOICE_DATA_FOR,
                    Locale.getDefault());
            startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
    

    In your onActivityResult add this code which checks if the tts engine is available or not

        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == MY_DATA_CHECK_CODE) {
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_FAIL) {
                //TTS is not installed for the default language
            }
    
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
    
                Toast.makeText(
                        getApplicationContext(),
                        "TTS Engine Data for default language is installed ",
                        Toast.LENGTH_LONG).show();
            } else {
    
           //TTS for default language not installed.check for other languages 
                if (data != null) {
                    ArrayList<String> availableLanguages = data
                            .getStringArrayListExtra(TextToSpeech.Engine.EXTRA_AVAILABLE_VOICES);
    
                    if (availableLanguages.isEmpty()) {
                        // no language data available, prompt for install
                        showDialog();
                    } else {
                        // some language data is available, create TTS instance
    
                        Toast.makeText(
                                getApplicationContext(),
                                "TTS Engine Data for default language is installed.",
                                Toast.LENGTH_LONG).show();
    
                    }
                }
    
            }