将语音识别器作为Google Glass中的后台服务运行

时间:2014-05-12 17:00:58

标签: google-glass google-gdk

我最近将Google Glass更新为最新的XE17版本。要使用Google进行语音识别,我们使用以下Intent

    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    startActivityForResult(intent, SPEECH_REQUEST);

但是,这会使用默认麦克风图标打开语音识别活动。我想避免这种情况,而是将语音识别功能作为Glass中的后台服务运行。

我知道如何在Android(移动设备)中执行此操作。然而,当我尝试在Glass中做同样的事情时,它没有用。

我的代码如下:

    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);        
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test");
    intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5); 
    sr.startListening(intent);

    class listener implements RecognitionListener          
 {
    public void onReadyForSpeech(Bundle params)
    {
        Log.d(TAG, "onReadyForSpeech");
    }
    public void onBeginningOfSpeech()
    {
        Log.d(TAG, "onBeginningOfSpeech");
    }
    public void onRmsChanged(float rmsdB)
    {
        Log.d(TAG, "onRmsChanged");
    }
    public void onBufferReceived(byte[] buffer)
    {
        Log.d(TAG, "onBufferReceived");
    }
    public void onEndOfSpeech()
    {
        Log.d(TAG, "onEndofSpeech");
    }
    public void onError(int error)
    {
        Log.d(TAG,  "error " +  error);

    }
    public void onResults(Bundle results)                   
    {
        String str = new String();
        Log.d(TAG, "onResults " + results);
        @SuppressWarnings("rawtypes")
        ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
        for (int i = 0; i < data.size(); i++){
            Log.d(TAG, "result " + data.get(i));
            str += data.get(i);

        }

    }
    public void onPartialResults(Bundle partialResults)
    {
        Log.d(TAG, "onPartialResults");
    }
    public void onEvent(int eventType, Bundle params)
    {
        Log.d(TAG, "onEvent " + eventType);
    }
      }

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

获取包裹如下:

intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getPackageName());

还要确保您设置了以下权限:

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET"/>