简体   繁体   中英

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

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.

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