简体   繁体   English

Android Text to Speech 非谷歌引擎

[英]Android Text to Speech non google engine

In my android app I have a TTS using Google engine.在我的 android 应用程序中,我有一个使用 Google 引擎的 TTS。

Have something like this:有这样的事情:

tts=new TextToSpeech(MyClass.this, status -> {
    if(status == TextToSpeech.SUCCESS){
       tts.setLanguage(locale);
       tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
            @Override
            public void onDone(String utteranceId) {

                if (utteranceId.equals("***")) {

                runOnUiThread(() -> {
                    Button view2 = findViewById(R.id.speech);
                    view2.setCompoundDrawablesWithIntrinsicBounds(R.drawable.play, 0, 0, 0);
                });
                }
            }

            @Override
            public void onError(String utteranceId) {
            }

            @Override
            public void onStart(String utteranceId) {
            }
        });

    }
});

Basically I am using 2 languages, slovak and english.基本上我使用两种语言,斯洛伐克语和英语。 Both are working fine with Google TTS.两者都适用于 Google TTS。

The problem is, Samsung devices have their own TTS engine set by default and therefore the app text to speech works not on those devices.问题是,三星设备默认设置了自己的 TTS 引擎,因此应用文本到语音无法在这些设备上运行。

After the users changes their device settings to use Google TTS, then it is working.在用户更改他们的设备设置以使用 Google TTS 后,它就可以工作了。

But is there a way, that my code will support both TTS engines?但是有没有办法让我的代码同时支持两个 TTS 引擎?

I found out that there might work something like this:我发现可能有这样的工作:

TextToSpeech(Context context, TextToSpeech.OnInitListener listener, String engine) 

eg using com.google.android.tts as the engine parameter.例如使用com.google.android.tts作为引擎参数。

However in my code I have that like new TextToSpeech(MyClass.this, status -> { ... and it doesn't accept engine as a 3rd parameter, and still I don't know how to detect when Samsung engine is needed and switch engines accordingly.但是在我的代码中,我有类似new TextToSpeech(MyClass.this, status -> { ... 并且它不接受引擎作为第三个参数,但我仍然不知道如何检测何时需要三星引擎和相应地切换引擎。

worth trying forcing TTS engine by passing this third param, so exchange very last line in posted snippet值得尝试通过传递第三个参数来强制 TTS 引擎,因此在发布的代码段中交换最后一行

});

to

}, "com.google.android.tts");

there are also two useful methods for you: getDefaultEngine() and getEngines() .还有两个有用的方法: getDefaultEngine()getEngines() just create at start some dummy new TextToSpeech with two params (empty listener) and check what possibilites you have.只需在开始时创建一些带有两个参数(空侦听器)的虚拟new TextToSpeech并检查您有什么可能性。

also getAvailableLanguages() and isLanguageAvailable(Locale loc) may be useful when Google engine isn't present, but default one still may support your desired langs当 Google 引擎不存在时, getAvailableLanguages()isLanguageAvailable(Locale loc)也可能很有用,但默认情况下仍然可以支持您想要的语言

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM