0

我正在尝试创建允许连续运行 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。有没有人遇到过这个问题并且可能知道如何解决这个问题?谢谢

4

1 回答 1

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);
    }
于 2014-07-08T17:26:17.653 回答