Google Glass和SpeechRecognizer类

时间:2014-01-15 21:35:11

标签: google-glass google-gdk

我一直在尝试在Google Glass上的活动中使用SpeechRecognizer类

我在摩托罗拉Razor上运行此代码,效果很好。 我没有成功在Glass上做这件事 “没有选定的语音识别服务”是

时我得到的错误

调用sr.startListening(intent);

我知道语音识别的activityForResult方法,但是我正在寻找能在我的Activity中运行的东西,谢谢。

    public class MainActivity extends Activity {

SpeechRecognizer sr;
TextView mText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    sr = SpeechRecognizer.createSpeechRecognizer(this);       
    sr.setRecognitionListener(new listener()); 


    mText = (TextView) findViewById(R.id.resultsText);
    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,this.getPackageName());

    intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5); 
         sr.startListening(intent);


}


class listener implements RecognitionListener          
   {
            private static final String TAG = "Speech";
            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);
                     mText.setText("error " + error);
            }
            public void onResults(Bundle results)                   
            {
                     String str = new String();
                     Log.d(TAG, "onResults " + results);
                     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);
                     }
                     mText.setText("results: "+String.valueOf(data.size()));        
            }
            public void onPartialResults(Bundle partialResults)
            {
                     Log.d(TAG, "onPartialResults");
            }
            public void onEvent(int eventType, Bundle params)
            {
                     Log.d(TAG, "onEvent " + eventType);
            }
   }
    }

2 个答案:

答案 0 :(得分:0)

尚未支持此SpeechRecognizer的用例;现在,您只能使用RecognizerIntent发起一项活动来转录语音。

请随时关注我们的问题跟踪器上的issue 245,以便您随着GDK的发展保持更新!

答案 1 :(得分:0)

如果您仍然遇到此问题,则应使用RecognizerIntent.ACTION_RECOGNIZE_SPEECH。

示例实现

private static final int SPEECH_REQUEST = 0;

// will show the microphone and lets user speak to capture speech
private void displaySpeechRecognizer() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    startActivityForResult(intent, SPEECH_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode,
        Intent data) {
    if (requestCode == SPEECH_REQUEST && resultCode == RESULT_OK) {
        List<String> results = data.getStringArrayListExtra(
                RecognizerIntent.EXTRA_RESULTS);
        // the first string in the results list is considered the best match.
        String spokenText = results.get(0);
        // Do something with spokenText.
    }
    super.onActivityResult(requestCode, resultCode, data);
}

您可以在以下网址了解详情:https://developers.google.com/glass/develop/gdk/voice?hl=en#starting_speech_recognitionhttp://developer.android.com/reference/android/speech/RecognizerIntent.html#ACTION_RECOGNIZE_SPEECH