Recognition listener android studio, it doesn't work

1.2k views Asked by At

I copied this from internet, other people say that works, but when I try to speak it doesn't do anything.

I tried to write the code about the intent outside the method onBeginingOfSpeech() and it works.

public class Schermata extends ActionBarActivity implements RecognitionListener{

    SpeechRecognizer speechRecognizer;
    TextView test;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_schermata);
        speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
        test = (TextView) findViewById(R.id.test);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_schermata, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onReadyForSpeech(Bundle params) {
        test.setText("INIZIO");

    }

    @Override
    public void onBeginningOfSpeech() {
        test.setText("INIZIO");
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,getClass().getPackage().getName());
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        speechRecognizer.startListening(intent);

    }

    @Override
    public void onRmsChanged(float rmsdB) {

    }

    @Override
    public void onBufferReceived(byte[] buffer) {

    }

    @Override
    public void onEndOfSpeech() {
        test.setText("FINE");
    }

    @Override
    public void onError(int error) {

    }

    @Override
    public void onResults(Bundle results) {
        ArrayList<String> res = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
        String s = "";
        for (String result : res)
            s += result + "\n";
        test.setText(s);
    }

    @Override
    public void onPartialResults(Bundle partialResults) {

    }

    @Override
    public void onEvent(int eventType, Bundle params) {

    }
}
2

There are 2 answers

0
Mary On
<uses-permission android:name="android.permission.RECORD_AUDIO" />

Ensure you have RECORD_AUDIO permission in the manifest, voice recognition doesn`t work without it.

0
tamir007 On

If I understood correctly and this works for you when the Intent is defined outside of onBeginningOfSpeech() , then simply put it outside of it, it is wrong to put it there, and I'll explain.

The function onBeginningOfSpeech() ( Also : onResults,onReadyForSpeech, etc...) is called AFTER the recognizer has started working, and is only a callback to the event where the recognizer hears a person speaking, so you need to have that intent ready and paired up to the recognizer BEFORE this function is called.

Try to create an 'initialize()' function, which sets all of those up before you call

speechRecognizer.startListening(intent);

In other words use this , instead of what you have on 'onBeginningOfSpeech()'

 public void initialize(){
    Intent intent = new   Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,getClass().getPackage().getName());
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    speechRecognizer.startListening(intent);
}

Other then that, you are using onResults() in the wrong way, you should only extract the first String of the ArrayList given to you, the others are just worst results of the same speech.