简体   繁体   中英

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):

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"

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...

Even stranger is that the InitListener always reports a "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".

And this is the actual code I use to run the 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
  • 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"?
  • 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.

I now moved that to the OnInit listener and presto: it works!

Here's my OnInitListener now:

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");
        }

    }

};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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