如何检查是否为印地语安装了Google TTS语音数据

时间:2015-01-04 17:33:12

标签: android

我正在使用谷歌TTS引擎进行印地语的文本转语音。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case ACTIVITY_TTS_PASS:
        if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
            tts = new TextToSpeech(getApplicationContext(), this);
            int availability = tts.isLanguageAvailable(new Locale("hin-IND"));
            switch(availability) {
            case TextToSpeech.LANG_AVAILABLE:
                Log.d("TTS", "Language available");
                break;
            case TextToSpeech.LANG_NOT_SUPPORTED:
                Log.d("TTS", "Language not supported");

                Intent installTTSIntent = new Intent();
                installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                ArrayList<String> languages = new ArrayList<String>();
                languages.add("hin-IND"); // hin - hindi, IND - INDIA
                installTTSIntent.putStringArrayListExtra(
                        TextToSpeech.Engine.EXTRA_CHECK_VOICE_DATA_FOR, languages);
                startActivity(installTTSIntent);
                break;
            case TextToSpeech.LANG_MISSING_DATA:
                Log.d("TTS", "Language missing data");
                break;
            case TextToSpeech.LANG_COUNTRY_AVAILABLE:
                Log.d("TTS", "Contry available");
                break;
            default:
                Log.d("TTS", "default");
            }
        }
        else {
            Log.d("TTS", "fail");
            Intent installTTSIntent = new Intent();
            installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            ArrayList<String> languages = new ArrayList<String>();
            languages.add("hin-IND"); // hin - hindi, IND - INDIA
            installTTSIntent.putStringArrayListExtra(
                    TextToSpeech.Engine.EXTRA_CHECK_VOICE_DATA_FOR, languages);
            startActivity(installTTSIntent);
        }

        break;
    default:
        Log.d("TTS", "case default");
        break;
    }
    super.onActivityResult(requestCode, resultCode, data);

}

这是我可以检查是否已安装TTS数据以及是否支持印地语的方法。 此Intent提供了为某些语言安装语音数据的选项。然后我选择印地语(印度)并安装。

即使在那之后     tts.isLanguageAvailable(new Locale(&#34; hin-IND&#34;)); 返回TextToSpeech.LANG_NOT_SUPPORTED。

还试过&#34; hi-IN&#34;相反&#34; hin-IND&#34;,它给出了相同的结果。 现在已安装语音数据,但我如何通过代码进行检查?

1 个答案:

答案 0 :(得分:1)

&#34; hi_IN&#34;是印地语(印度)的地方。检查可用性tts.isLanguageAvailable(新的Locale(&#34; hi-IN&#34;))在调用onInit(int result)方法之后正常工作。因此,在onActivityResult方法中启动tts实例并检查onInit方法中的可用性。

2017年更新 根据要求提供代码以使用TTS。

检查是否存在tts数据:

Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);

将调用OnActivityResult方法

protected void onActivityResult(
        int requestCode, int resultCode, Intent data) {
    if (requestCode == MY_DATA_CHECK_CODE) {
        if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
            // success, create the TTS instance
            initTTS();
        } else {
            // missing data, install it
            Intent installIntent = new Intent();
            installIntent.setAction(
                TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            ArrayList<String> languages = new ArrayList<String>();
            languages.add("hin-IND"); // hin - hindi, IND - INDIA
            installIntent.putStringArrayListExtra(
                    TextToSpeech.Engine.EXTRA_CHECK_VOICE_DATA_FOR, languages);
            startActivity(installIntent);
        }
    }
}

initTTS方法如下:

private TextToSpeech mTts;
public void initTTS() {
    //mTts = new TextToSpeech(this, this);
    mTts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if(status != TextToSpeech.ERROR) {

                int isLanguageAvailable = t1.isLanguageAvailable(new Locale("hi_IN"));
                switch (isLanguageAvailable) {
                    case TextToSpeech.LANG_AVAILABLE:
                        //t1.setLanguage(Locale.UK);
                        t1.setLanguage(new Locale("hin"));
                        t1.setPitch(1.2f);
                        t1.setSpeechRate(0.8f);
                        Toast.makeText(context, "हाँ, मैं हिंदी बोल सकता हूँ", Toast.LENGTH_LONG).show();
                        t1.speak("हाँ, मैं हिंदी बोल सकता हूँ", TextToSpeech.QUEUE_FLUSH, null,null);

                        break;
                }
            } else {
                Toast.makeText(context, "TTS initialization Failed", Toast.LENGTH_LONG).show();
            }
        }
    });
}