我正在尝试使用Google的语音识别功能来构建应用程序。下面是我这个项目的代码。问题是,当用户点击扬声器按钮(提示谷歌语音识别器的图像按钮)并开始说话时,如果用户说话有点长,那么“点击讲话”会保持冻结,这意味着没有收到结果(虽然如果我触摸手机,它就可以接收用户输入了)
然而,当我使用安装在智能手机中的Google应用程序(我认为是工厂版)时,即使我说了一个长句(有10-15个字甚至更多),它也能很好地检测到我的声音。
我如何实现此功能,或者谷歌是否使用与Android中的Google语音识别器不同的引擎?
以下是我的代码!
private void promptSpeechInput() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
//intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,"en-US");
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 200000);
intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, 200000);
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String tem_result = "";
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txtSpeechInput.setText(result.get(0));
if (result.get(0).toLowerCase().equals(examples[index]))
{
txtSpeechInput.setText("correct:" + result.get(0).toLowerCase());
index += 1;
txtSpeakThis.setText(examples[index]);
}
else
{
txtSpeechInput.setText("wrong:" + result.get(0).toLowerCase());
}
}
else if ( resultCode == RecognizerIntent.RESULT_CLIENT_ERROR){
Toast.makeText(this,"client error", Toast.LENGTH_LONG).show();
}
else if ( resultCode == RecognizerIntent.RESULT_AUDIO_ERROR){
Toast.makeText(this,"RESULT_AUDIO_ERROR", Toast.LENGTH_LONG).show();
}
else if ( resultCode == RecognizerIntent.RESULT_NETWORK_ERROR){
Toast.makeText(this,"RESULT_NETWORK_ERROR", Toast.LENGTH_LONG).show();
}
else if ( resultCode == RecognizerIntent.RESULT_NO_MATCH){
Toast.makeText(this,"RESULT_NO_MATCH", Toast.LENGTH_LONG).show();
}
else if ( resultCode == RecognizerIntent.RESULT_SERVER_ERROR){
Toast.makeText(this,"RESULT_SERVER_ERROR", Toast.LENGTH_LONG).show();
}
break;
}
}
}