【问题标题】:How to activate speech to text with a button?如何使用按钮将语音激活为文本?
【发布时间】:2013-06-24 03:18:09
【问题描述】:

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

谢谢

【问题讨论】:

标签: android speech-to-text


【解决方案1】:

如果您尚未检查Api demos 中的Voice Recognition 样本,您应该继续检查它。它应该给你一个良好的开端。演示可在/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

希望这会有所帮助。

【讨论】:

  • 谢谢,我去看看。
  • 不错! #2 完全为我工作。有趣,因为我们的 Android 手机键盘中已经有一个语音激活的麦克风图标。例如,当您在应用程序中单击EditText 时,如果您单击键盘上的麦克风图标,它会自动开始将您的声音听写到您的EditText。根本不需要任何代码!但是,并不是每个人都知道这一点,并且在 UI 的小范围内用一句话来解释它会很尴尬,所以这种方法很有效!谢谢!
【解决方案2】:

在您的应用中,您使用 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);
}

更多信息可以在reference找到

【讨论】:

    【解决方案3】:
    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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-07
      • 1970-01-01
      • 1970-01-01
      • 2015-03-27
      • 1970-01-01
      • 1970-01-01
      • 2014-01-07
      • 2019-12-19
      相关资源
      最近更新 更多