gre*_*egm 12
让我剪切并粘贴一下,以显示您需要的代码.
编辑:您也可以从这个项目下载一个方便的抽象类.
您将需要此意图(根据需要进行参数化):
public Intent getRecognizeIntent(String promptToUse, int maxResultsToReturn)
{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, maxResultsToReturn);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, promptToUse);
return intent;
}
Run Code Online (Sandbox Code Playgroud)
然后你需要将你的意图发送到语音识别活动,如此,
public void gatherSpeech(String prompt)
{
Intent recognizeIntent = getRecognizeIntent(prompt);
try
{
startActivityForResult(recognizeIntent, SpeechGatherer.VOICE_RECOGNITION_REQUEST_CODE);
}
catch (ActivityNotFoundException actNotFound)
{
Log.w(D_LOG, "did not find the speech activity, not doing it");
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您需要让您的活动处理语音结果:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
Log.d("Speech", "GOT SPEECH RESULT " + resultCode + " req: "
+ requestCode);
if (requestCode == SpeechGatherer.VOICE_RECOGNITION_REQUEST_CODE)
{
if (resultCode == RESULT_OK)
{
ArrayList<String> matches = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
Log.d(D_LOG, "matches: ");
for (String match : matches)
{
Log.d(D_LOG, match);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7585 次 |
| 最近记录: |