适用于Android的语音识别/听写

时间:2017-04-06 09:39:20

标签: java android speech-recognition

美好的一天,

我正处于建立烹饪/食谱应用程序的早期阶段。该应用程序的主要目的是能够使用语音听写来跟踪和遍历食谱。任何人都可以指出我如何实现这些功能的正确方向?

谢谢!

1 个答案:

答案 0 :(得分:1)

调用系统内置的语音识别器活动以获取用户的语音输入。这对于从用户获取输入然后进行处理非常有用,例如进行搜索或将其作为消息发送。

在您的应用中,您使用ACTION_RECOGNIZE_SPEECH操作调用startActivityForResult()。这将启动语音识别活动,然后您可以在onActivityResult()中处理结果。

private static final int SPEECH_REQUEST_CODE = 0;

// Create an intent that can start the Speech Recognizer activity
private void displaySpeechRecognizer() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
// Start the activity, the intent will be populated with the speech text
    startActivityForResult(intent, SPEECH_REQUEST_CODE);
}

// This callback is invoked when the Speech Recognizer returns.
// This is where you process the intent and extract the speech text from the intent.
@Override
protected void onActivityResult(int requestCode, int resultCode,
        Intent data) {
    if (requestCode == SPEECH_REQUEST_CODE && resultCode == RESULT_OK) {
        List<String> results = data.getStringArrayListExtra(
                RecognizerIntent.EXTRA_RESULTS);
        String spokenText = results.get(0);
        // Do something with spokenText
    }
    super.onActivityResult(requestCode, resultCode, data);
}