3

我正在创建一个使用 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);
        }
    }
}

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

我已经做了一些研究,但我正在努力寻找可以让我实现这一目标的代码示例。

4

1 回答 1

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

于 2013-08-08T20:08:10.373 回答