我想实现一个按钮,当点击该按钮时,它将激活android的语音到文本转换器,就像android键盘提供的一样。具体来说,我想要一个按钮,让应用程序实时转录用户所说的内容,并逐字(实时)记录在editText框中。做这件事最好的方法是什么?
感谢
如果您还没有检查Api demos
中的Voice Recognition
样本,您应该继续检查它。它应该会让您领先一步。演示可在/android-sdk/samples/...
文件夹中找到。如果你还没有安装它们,下面是如何将android api演示应用程序安装到我的手机中。
以下(任何其他)教程也将帮助您开始:
1) 安卓语音识别教程
2) 安卓系统:使用API 的语音转文本
以下可能也是一个不错的阅读:
将文本到语音和语音识别添加到您的Android应用程序并使用Android语音识别API。
希望这能有所帮助。
在应用程序中,使用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);
}
更多信息可以在参考中找到
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);
}