1
votes

I've been trying to use SpeechRecognizer class on an activity on Google Glass

I run this code on a Motorola Razor and it works well. I have not been successful doing this on Glass "no selected voice recognition service" is the error I get back when

sr.startListening(intent) is called;

I am aware of the activityForResult methods of voice recognition, However I'm looking for something that will run within my Activity, Thanks.

    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

2 Answers

0
votes

This use case of SpeechRecognizer is not yet supported; right now, you can only launch an activity using RecognizerIntent to transcribe speech.

Please feel free to follow issue 245, which covers this, on our issue tracker so that you can stay updated as the GDK evolves!

0
votes

If you are still have this problem, you should use RecognizerIntent.ACTION_RECOGNIZE_SPEECH.

Example implementation is

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

You can read more at : https://developers.google.com/glass/develop/gdk/voice?hl=en#starting_speech_recognition and http://developer.android.com/reference/android/speech/RecognizerIntent.html#ACTION_RECOGNIZE_SPEECH