相关疑难解决方法(0)

如何轮询可用的TTS引擎以获取可用的语言,而无需实例化每个语言并使用init

我需要实例化一个TextToSpeech对象并设置一个给定的语言(以编程方式设置,可能会有所不同).我知道我可以使用setLanguage(),但只有在TTS引擎中有特定TextToSpeech实例使用的语言时才能使用.我知道我可以通过myTTS.isLanguageAvailable()检查语言是否可用,但这只会告诉我当前引擎上是否有该语言可用.

问题是用户可能安装了多个TTS引擎,并且所需语言可能在其中一个中可用,但在默认语言中不可用.在这种情况下,我想找到引擎,使用它并设置语言.

因此,我需要遍历可用的TTS引擎并"询问"每个引擎是否具有所需的语言.

我试过这个:

mUserLocale=new Locale("it-IT"); //just an example

mTextToSpeech=new TextToSpeech(getApplicationContext(), this);
if (mTextToSpeech.isLanguageAvailable(mUserLocale)<0) {

    List<TextToSpeech.EngineInfo> engines=mTextToSpeech.getEngines();
    int currentmatchquality=-1;
    String defaultTTSEngine=mTextToSpeech.getDefaultEngine();
    mTextToSpeech.shutdown();
    mTextToSpeech=null;
    for (int i=0; i<engines.size(); i++) {
        TextToSpeech.EngineInfo engineinfo=engines.get(i);
        Log.d("MainActivity", "Examining TTS engine "+engineinfo.name);
        if (engineinfo.name.equals(defaultTTSEngine)) {
            Log.d("MainActivity", "Skipping default TTS engine "+engineinfo.name);
            continue;
        }
        TextToSpeech candidateTTS=new TextToSpeech(getApplicationContext(),this,engineinfo.name);
        int matchquality=candidateTTS.isLanguageAvailable(mUserLocale);
        if (matchquality>currentmatchquality) {
            Log.d("MainActivity", "Selecting TTS engine "+engineinfo.name);
            mTextToSpeech.shutdown();
            mTextToSpeech=candidateTTS;
            mTTSEngine=engineinfo.name;
            currentmatchquality=matchquality;

        }
        else {
            Log.d("MainActivity", "   "+mUserLocale.toString()+" not available on this engine: "+matchquality);
        }
    }
    if (mTTSEngine==null) …
Run Code Online (Sandbox Code Playgroud)

android text-to-speech

6
推荐指数
1
解决办法
2067
查看次数

标签 统计

android ×1

text-to-speech ×1