How to check if a specific language data for Text to Speech(TTS) is installed on a device?

北城余情 提交于 2019-12-10 19:06:44

问题


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.


回答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



来源:https://stackoverflow.com/questions/18134106/how-to-check-if-a-specific-language-data-for-text-to-speechtts-is-installed-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!