简体   繁体   English

Nexus 7上的android文本到语音0支持的语言

[英]android text to speech 0 supported languages on nexus 7

I'm working on a little app that gives the user the option to "speak" a couple of commands to which the app will respond. 我正在开发一个小应用程序,它为用户提供了“说出”该应用程序将响应的几个命令的选项。

I've got the speech recognition working and the commands registered. 我已经完成了语音识别功能并注册了命令。 They are being recognized and by all means, the answer should be played. 他们正在被认可,并且应该以所有方式播放答案。

Only issue is: it doesn't. 唯一的问题是:事实并非如此。

here's the bit of code I use to determine what languages are supported by google's tts engine (I'm developing for an older version of android so "tts.getAvailableLangues()" isn't in there unfortunately): 这是我用来确定Google的tts引擎支持哪些语言的代码段(很遗憾,我正在开发较旧版本的android,因此“ tts.getAvailableLangues()”不存在):

Locale[] locales = Locale.getAvailableLocales();
    List<Locale> localeList = new ArrayList<Locale>();
    for (Locale locale : locales) {
        int res = speaker.isLanguageAvailable(locale);
        if (res == TextToSpeech.LANG_COUNTRY_AVAILABLE) {
            Log.d(tag, "language: " + locale);
            localeList.add(locale);
        }
    }
    Log.d(tag, "languages available: " + localeList.size());

The output of that last log is always: "languages available: 0" 最后一个日志的输出始终为:“可用语言:0”

And indeed, if I run this bit of code: 实际上,如果我运行以下代码:

int id = speaker.setLanguage(Locale.ENGLISH);
checkId(id);

It'll come back with a "language not supported" error. 它将返回“不支持语言”错误。 Which is odd, given that I'm only trying to use one of the default languages: English (UK, US, doesn't matter, it's "not supported"). 考虑到我仅尝试使用一种默认语言,这很奇怪:英语(英语,美国,没关系,“不支持”)。

So, obviously, when I try to run 所以,很明显,当我尝试跑步时

speaker.getLanguage();

it returns null... 它返回null ...

Even stranger is that the InitListener always reports a "TextToSpeech.SUCCESS" 甚至更奇怪的是,InitListener始终报告“ TextToSpeech.SUCCESS”

private OnInitListener listener = new OnInitListener() {

    @Override
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            Log.d(TAG, "tts engine started succesfully");
        } else if (status == TextToSpeech.ERROR) {
            Log.d(TAG, "seems like an error occured :c");
        }

    }

};

So the log always shows: "tts engine started succesfully". 因此,日志始终显示:“ tts引擎成功启动”。

And this is the actual code I use to run the tts: 这是我用来运行tts的实际代码:

public void speak(String text, Context context, OnInitListener listener) {
    String tag = "dashboardactivity";
    Log.d(tag, "speaking started in dummy module");
    TextToSpeech speaker = new TextToSpeech(context, listener);

    // int id = speaker.setLanguage(Locale.UK);
    int id = speaker.setLanguage(Locale.ENGLISH);

    speaker.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}

There doesn't seem to be anything wrong with that code, or am I missing something? 该代码似乎没有什么问题,还是我错过了什么?

Here's a list of things i've tried/checked/installed: 这是我尝试过/检查过/安装过的东西的清单:

  • tts is installed 已安装tts
  • voice data is installed (multiple voices for UK and US English) 已安装语音数据(英国和美国英语有多种语音)
  • if I press the "listen to an example" button in the settings it works! 如果我在设置中按“听一个例子”按钮,它将起作用!

So, from where I'm sitting there's no reason it shouldn't be working. 因此,从我坐在那里开始,没有理由它不应该工作。 My question are then: 我的问题是:

  • am i missing something? 我想念什么吗? (special permissions? some data?) (特殊权限?一些数据?)
  • why does my code say that the tts engine supports 0 languages out of the 400-something "locales"? 为什么我的代码说tts引擎支持400多种“语言环境”中的0种语言?
  • how can i fix this? 我怎样才能解决这个问题?

ok, I fixed it. 好的,我修复了它。 I didn't think about the time the tts engine needs to initialize itself so I simply called "speak" right after instantiating it. 我没有考虑过tts引擎需要进行自我初始化的时间,因此我在实例化它之后立即调用了“讲话”。

I now moved that to the OnInit listener and presto: it works! 现在,我将其移至OnInit侦听器并预先保存:它可以正常工作!

Here's my OnInitListener now: 现在是我的OnInitListener:

private OnInitListener listener = new OnInitListener() {

    @Override
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            Log.d(TAG, "tts engine started succesfully");
            Log.d(TAG, "setting language to default");
            int id = tts.setLanguage(Locale.getDefault());
            checkId(id);
            tts.speak(mMessageToSpeak, TextToSpeech.QUEUE_FLUSH, null);
        } else if (status == TextToSpeech.ERROR) {
            Log.d(TAG, "seems like an error occured :c");
        }

    }

};

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

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