我正在尝试创建允许连续运行Android语音识别引擎的服务。我在Android Speech Recognition as a service on Android 4.1 & 4.2看到了一个示例,并且接受了它,但不知怎么说,我总是在侦听器onError()方法中得到SpeechRecognizer.ERROR_NO_MATCH错误。当然,永远不会调用onResults()。该服务的代码与上面链接的答案完全相同,我的代码是:
Intent startServiceIntent = null;
Log.d(TAG, "Creating new intent for the recognition service");
startServiceIntent = new Intent(getApplicationContext(), SpeechRecognitionService.class);
Log.d(TAG, "Starting the speech recognition service ...");
getApplicationContext().startService(startServiceIntent);
int duration = Toast.LENGTH_LONG;
CharSequence text = "Starting speech recognition ...";
Toast toast = Toast.makeText(getApplicationContext(), text, duration);
toast.show();
但是当我通过发送Intent直接调用语音识别引擎时,它运行得很好:
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Start talking");
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 10);
startActivityForResult(intent, 1234);
我所有的测试我在Note2上运行Android 4.1.2。有没有人遇到这个问题,可能知道如何解决这个问题? 感谢
答案 0 :(得分:0)
尝试设置EXTRA_CALLING_PACKAGE。它没有意义,但我相信它会解决你的问题。请参阅以下方法。另请查看此处的代码:https://github.com/gast-lib/gast-lib/blob/master/library/src/root/gast/speech/SpeechRecognizingActivity.java
public void recognizeDirectly(Intent recognizerIntent)
{
// SpeechRecognizer requires EXTRA_CALLING_PACKAGE, so add if it's not
// here
if (!recognizerIntent.hasExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE))
{
recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
"com.dummy");
}
SpeechRecognizer recognizer = getSpeechRecognizer();
recognizer.startListening(recognizerIntent);
}