Android语音识别更改返回语言

时间:2019-10-10 11:14:09

标签: java android localization speech-to-text

我正在开发一个Android应用,该应用接受语音输入并将结果文本设置为TextView。尽管要识别的语音语言不是英语,但该应用提供的结果文本是英语。详细地说,我正在创建的语音识别意图是这样的:

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

RecognitionListener onResult()方法如下:

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

我希望results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)返回一个由孟加拉字母组成的ArrayList。

预期的行为:

want image like this

当前输出:

wrong display image

1 个答案:

答案 0 :(得分:1)

使用以下方法来获得所需的输出,设置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) {

        }
    }

然后使用onActivityResult()来接收结果,并使用与startActivityForResult()中相同的请求代码,其目的如下:

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