Maxim Toyberman
Maxim Toyberman

Reputation: 2006

Speech recognition supported languages on Android

I'm having a problem getting the supported languages. I have seen a solution that is to create a Broadcast receiver and fill the list with the languages.

public class LanguageChecker extends BroadcastReceiver
{
    private List<String> supportedLanguages;

    private String languagePreference;

    @Override
    public void onReceive(Context context, Intent intent)
    {
        //Log.d(Constants.Tag,"OnReceive");
        Bundle results = getResultExtras(true);
        if (results.containsKey(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE))
        {
            languagePreference =
                    results.getString(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE);
        }
        if (results.containsKey(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES))
        {
            supportedLanguages =
                    results.getStringArrayList(
                            RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES);


        }
    }

    public List<String> getSupportedLanguages() {
        return supportedLanguages;
    }
}

but the problem is that I need this supportedLanguages list to fill my spinner. When I call the method getSupportedLanguages, I get null.

This is how I use the broadcast within onCreate:

try {
    lang = new LanguageChecker();
    Intent detailsIntent = new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS);
    sendOrderedBroadcast(detailsIntent, null, lang, null, Activity.RESULT_OK, null, null);


    supportedLanguagesForSpeech=lang.getSupportedLanguages();
    Log.d(Constants.Tag, String.valueOf(supportedLanguagesForSpeech.size()));
}
catch (Exception e){
    Log.d(Constants.Tag,e.getMessage());
}

Upvotes: 1

Views: 1054

Answers (1)

Kaarel
Kaarel

Reputation: 10672

You should solve it with a callback to make sure that supportedLanguages is assigned. You are getting null because you are not waiting onReceive to be called.

Here is my current solution to querying all the available speech recognition services for their supported languages:

https://github.com/Kaljurand/K6nele/blob/3e514edc87e07babb0be57fa31ab48be7e2226e7/app/src/ee/ioc/phon/android/speak/RecognitionServiceManager.java

You can see it in action in the Kõnele app (http://kaljurand.github.io/K6nele/about/), in the "Settings -> Recognition languages & services" list.

Upvotes: 1

Related Questions