【发布时间】:2013-06-24 03:18:09
【问题描述】:
我想实现一个按钮,单击该按钮会激活 android 的语音到文本翻译器,就像 android 的键盘提供的那样。具体来说,我想要一个按钮,让应用程序实时转录用户所说的内容,并在editText框中逐字(实时)记录。这样做的最佳方法是什么?
谢谢
【问题讨论】:
我想实现一个按钮,单击该按钮会激活 android 的语音到文本翻译器,就像 android 的键盘提供的那样。具体来说,我想要一个按钮,让应用程序实时转录用户所说的内容,并在editText框中逐字(实时)记录。这样做的最佳方法是什么?
谢谢
【问题讨论】:
如果您尚未检查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 Applications 和 Using the Android Speech Recognition APIs。
希望这会有所帮助。
【讨论】:
EditText 时,如果您单击键盘上的麦克风图标,它会自动开始将您的声音听写到您的EditText。根本不需要任何代码!但是,并不是每个人都知道这一点,并且在 UI 的小范围内用一句话来解释它会很尴尬,所以这种方法很有效!谢谢!
在您的应用中,您使用 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找到
【讨论】:
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);
}
【讨论】: