如何用按钮激活语音到文本?

时间:2013-06-24 03:18:10

标签: android speech-to-text

我想实现一个按钮,当点击它会激活android的语音到文本翻译器,就像android的键盘提供的那样。具体来说,我想要一个按钮,让应用程序实时转录用户所说的内容,并在editText框中逐字(实时)记录。这样做最好的方法是什么?

由于

3 个答案:

答案 0 :(得分:2)

如果您尚未检查Voice Recognition中的Api demos示例,则应继续检查。它应该给你一个良好的开端。演示文稿位于/android-sdk/samples/...文件夹中。如果您尚未安装它们,请按以下方式how to install android api demo app into my phone进行操作。

以下(还有许多其他)教程也将帮助您开始:

1)Android Voice Recognition Tutorial

2)Android: Speech To Text using API

以下可能是一个很好的阅读:

Add Text-To-Speech and Speech Recognition to Your Android ApplicationsUsing the Android Speech Recognition APIs

希望这会有所帮助。

答案 1 :(得分:0)

在您的应用中,您使用startActivityForResult()操作致电ACTION_RECOGNIZE_SPEECH。这将启动语音识别活动,然后您可以在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);
}

可以在reference

中找到更多信息

答案 2 :(得分:0)

private void startVoiceRecognitionActivity()
{
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Voice recognition Demo...");
    startActivityForResult(intent, REQUEST_CODE);
}

/**
 * Handle the results from the voice recognition activity.
 */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)
    {
        // Populate the wordsList with the String values the recognition engine thought it heard
        ArrayList<String> matches = data.getStringArrayListExtra(
                RecognizerIntent.EXTRA_RESULTS);
        myEditText.setText(matches.get(0));
    }
    super.onActivityResult(requestCode, resultCode, data);
}