androidspeech-recognitionrecognizer-intent

Android RecognizerIntent Speech recognition


How to handle the visibility of an image(ImageView) in the event the RecognizerIntent finishes due to the user not speaking

if (RecognizerIntent.EXTRA_RESULTS == null){
image1.setVisibility(View.VISIBLE);///microphone icon
}

or

if (RecognizerIntent.ACTION_RECOGNIZE_SPEECH == null){
image1.setVisibility(View.INVISIBLE);///microphone
}

thnx


Solution

  • In the code above you are just testing if the constants are null, which they are not. You have to start the recognition somewhere in the code by

        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        //... put other settings in the Intent 
        startActivityForResult(intent, REQUEST_CODE);
    

    and receive the result in

         @Override
         protected void onActivityResult(int requestCode, int resultCode, Intent data)
         {
           if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
             ArrayList<String> results = data.getStringArrayListExtra(
                    RecognizerIntent.EXTRA_RESULTS);
              //... do your stuf with results
             }
         super.onActivityResult(requestCode, resultCode, data);
         }
    

    A more customizable way is to use SpeechRecognizer directly. See for example this question.