Android Speech Recognision change return language

Viewed 214

I'm working on an android app which takes voice input and sets the result text into a TextView. Though the language of the speech to be recognized is not English, the result text the app is providing is English. To put it in an elaborate way, the speech recognition intent I am creating is like this:

Locale locale = new Locale.Builder().setLanguage("bn").setScript("Beng").setRegion("BD").build();
intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,locale);
intent.putExtra(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES,locale);
intent.putExtra(RecognizerIntent.EXTRA_ONLY_RETURN_LANGUAGE_PREFERENCE,locale);

And the RecognitionListener onResult() method looks like this:

public void onResults(Bundle results) {
    ArrayList<String> voiceResults = results
            .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
    if (voiceResults == null) {
        text = "";
        Log.e("Listener","No voice results");
    } else {
        text = voiceResults.get(0);
        display.setText(text);
    }
}

I want the results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION) to return an ArrayList consisting of Bengali letters.

The Expected behavior:

want image like this

Current output:

wrong display image

1 Answers

Use the following method to achieve the desired output, set bn-BD:

 private void getAudioInput() {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "bn-BD");
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "I am Listening...");
        try {
            startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
        } catch (ActivityNotFoundException ignored) {

        }
    }

Then use onActivityResult() to receive the result using the same Request Code that you've used in the startActivityForResult() with the intent like this:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQ_CODE_SPEECH_INPUT) {
            if (resultCode == RESULT_OK && null != data) {
                ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                if (result != null) {
                    bangla_text = result.get(0);
                    textOutput.setText(bangla_text);
                }
            }
        }
    }
Related