4

I am creating an app which uses Text to Speech and I want the user to have the ability to use it offline so I make a check to see if the TTS data is installed on the device, here is thde code which does this:

// Check tts data is installed
Intent checkTTSIntent = new Intent();
checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);

protected void onActivityResult(int requestCode, int resultCode, Intent data){
    if(requestCode == MY_DATA_CHECK_CODE){
        if(resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS){
            tts = new TextToSpeech(this, this);
        }
        else{
            Intent installTTSIntent = new Intent();
            installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            startActivity(installTTSIntent);
        }
    }
}

But this prompts the user to install German, Spanish, French and Italian as four download options, how can I just just check that one language is installed such as Italian?

I have done some research but I am struggling to find code examples which would allow me to achieve this.

Brtle
  • 2,297
  • 1
  • 19
  • 26
deucalion0
  • 2,422
  • 9
  • 55
  • 99

1 Answers1

1

Use EXTRA_CHECK_VOICE_DATA_FOR

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
    if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
        tts = new TextToSpeech(this, this);
    }
    else {
        Intent installTTSIntent = new Intent();
        installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
        ArrayList<String> languages = new ArrayList<String>();
        languages.add("it-IT"); // non sure if "it" is the right abbr for italian
        installTTSIntent.putStringArrayListExtra(TextToSpeech.Engine.EXTRA_CHECK_VOICE_DATA_FOR, 
                                                    languages);
        startActivity(installTTSIntent);
    }
    }
}

Document link http://developer.android.com/reference/android/speech/tts/TextToSpeech.Engine.html#EXTRA_CHECK_VOICE_DATA_FOR

Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54
  • I am trying this but cannot seem to get it to work, nothing happens, I will keep trying! Thank you!!! – deucalion0 Aug 08 '13 at 20:23
  • I know for sure that for English when put "eng-USA" there is no prompt. But if removing the put extra for EXTRA_CHECK_VOICE_DATA_FOR then it will prompt to install German, Spanish, French and Italian. Maybe the abbr for Italian is "ita"? – Hoan Nguyen Aug 08 '13 at 20:33
  • Hi there I tried it-IT and ita-ITA but even if Italian is installed it still prompts to download, it seems to be close to the solution though! :) Thank you! – deucalion0 Aug 08 '13 at 20:35
  • Hi there I tried it and still cannot get it to work, I am not sure this is possible :( Thank you for your help!!! – deucalion0 Aug 08 '13 at 21:31
  • From the document above, I think you should just initialize the TextToSpeech and then onInit if status is SUCCESS call isLanguageAvailable(Locale.ITALY) to see if Italian language is available or not. If not then do the check data. – Hoan Nguyen Aug 09 '13 at 06:22