如何检查设备上是否安装了文本到语音(TTS)的特定语言数据?

时间:2013-08-08 19:07:52

标签: android text-to-speech

我正在创建一个使用Text to Speech的应用程序,我希望用户能够脱机使用它,所以我检查一下是否在设备上安装了TTS数据,这里有代码执行此操作:

// 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);
        }
    }
}

但是这会提示用户安装德语,西班牙语,法语和意大利语作为四个下载选项,我怎样才能检查是否安装了一种语言,如意大利语?

我做了一些研究,但我很难找到能让我实现这一目标的代码示例。

1 个答案:

答案 0 :(得分:1)

使用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);
    }
    }
}

文档链接http://developer.android.com/reference/android/speech/tts/TextToSpeech.Engine.html#EXTRA_CHECK_VOICE_DATA_FOR