【问题标题】:How to use in-built voice recognition in my Android app?如何在我的 Android 应用程序中使用内置语音识别?
【发布时间】:2017-06-19 16:39:22
【问题描述】:

我想在我的应用中使用离线语音识别。 设置 -> “语言和输入法” -> “谷歌语音输入” -> “离线语音识别” :- 我想使用这个内置功能。 在下面的代码中,我尝试使用识别器意图来实现它,但它使用谷歌语音搜索(在线工作)。请帮忙。

public class MainActivity extends AppCompatActivity {

private TextView txtSpeechInput;
private ImageButton btnSpeak;
private final int REQ_CODE_SPEECH_INPUT = 100;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    promptSpeechInput();
    //onCreateOptionsMenu();
}

private void promptSpeechInput() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
            getString(R.string.speech_prompt));
    try {
        startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
    } catch (ActivityNotFoundException a) {
        Toast.makeText(getApplicationContext(),
                getString(R.string.speech_not_supported),
                Toast.LENGTH_SHORT).show();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
        case REQ_CODE_SPEECH_INPUT: {
            if (resultCode == RESULT_OK && null != data) {

                ArrayList<String> result = data
                        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                //txtSpeechInput.setText(result.get(0));
                {
                    String a="camera";
                    if(a.compareTo(result.get(0))==0){
                        Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                        if (i.resolveActivity(getPackageManager()) != null) {
                            startActivityForResult(i, 1);
                        }
                    }
                }
            }
            break;
        }

    }
}

好吧,我在这里通过语音向相机发送意图。所以,我在这里的词汇量要求有限。

【问题讨论】:

标签: java android speech-recognition offline


【解决方案1】:

试试这个:

intent.putExtra(RecognizerIntent.EXTRA_PREFER_OFFLINE,true);

注意:这仅适用于 API 级别 23 及更高级别。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多