2014-01-15 40 views
1

我一直試圖在谷歌眼鏡谷歌眼鏡和SpeechRecognizer類

我運行一個摩托羅拉剃刀此代碼,它的工作原理以及使用SpeechRecognizer類的活動。 我還沒有成功的玻璃 這樣做「沒有選擇的語音識別服務」是 等我回來的時候

sr.startListening(意向)被稱爲錯誤;

我知道activityForResult語音識別的方法,但我正在尋找的東西,將運行在我的活動,謝謝。

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); 
      } 
    } 
    } 

回答

0

該用例的SpeechRecognizer尚未支持;現在,您只能使用RecognizerIntent發起活動來轉錄語音。

請隨時關注我們的問題跟蹤器中的issue 245,這樣您就可以隨着GDK的發展而保持更新!

+0

我在想,如果現在支持SpeechRecognizer? – rottenoats

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