user3083522
user3083522

Reputation: 211

Android SpeechRecognizer service doesn't invoke listeners

My code for implementing speech recognition using SpeechRecognizer class seem correct....yet it does not function whatsoever. None of the log statements in any of the RecognitionListener methods are being logged.

I have follow the Android reference docs on this quite carefully and I am not seeing what I am missing.

I have added the record audio permission.

package com.example.voicetest;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class VoiceActivity extends Activity {


SpeechRecognizer recognizer;
TextView display;
private boolean listening;
private Button button1;

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

    listening=false;
    display=(TextView)findViewById(R.id.textView1);
    button1=(Button)findViewById(R.id.button1);





    recognizer=SpeechRecognizer.createSpeechRecognizer(this);

    recognizer.setRecognitionListener(
                new RecognitionListener()
                {

                    @Override
                    public void onBeginningOfSpeech() {
                        // TODO Auto-generated method stub
                        Log.i("test","onBeginning");
                    }

                    @Override
                    public void onBufferReceived(byte[] arg0) {
                        // TODO Auto-generated method stub
                        Log.i("test","onBuffer");
                    }

                    @Override
                    public void onEndOfSpeech() {
                        // TODO Auto-generated method stub
                        Log.i("test","onEndOfSpeech");
                    }

                    @Override
                    public void onError(int error) {
                        // TODO Auto-generated method stub
                        Log.i("test","onError");
                    }

                    @Override
                    public void onEvent(int eventType, Bundle params) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onPartialResults(Bundle partialResults) {
                        // TODO Auto-generated method stub
                        Log.i("test","onPartial");
                    }

                    @Override
                    public void onReadyForSpeech(Bundle params) {
                        // TODO Auto-generated method stub
                        display.setText("Ready and waiting..");
                        Log.i("test","onReady");
                    }

                    @Override
                    public void onResults(Bundle results) {
                        // TODO Auto-generated method stub
                        Log.i("test","onResults");
                        String result=stringListToString(results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION));

                        display.setText(result);

                    }

                    @Override
                    public void onRmsChanged(float rmsdB) {
                        // TODO Auto-generated method stub

                    }

                }
            );




}

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

public void buttonOnClick(View v)
{
    if(!listening)
    {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);        
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5); 
             recognizer.startListening(intent);
             Log.i("test","recognizer listening");

        listening=true;
        button1.setText("Stop Listening");

    }
    else
    {
        recognizer.stopListening();
        listening=false;
        Log.i("test","recognizer stopped");
        button1.setText("Listen");
    }
}

private String stringListToString(ArrayList<String> strings)
{
    String result="";

    for(int i=0;i<strings.size();i++)
    {
        result=result+strings.get(i)+'\n';
    }

    return result;
}

@Override
public void onPause()
{
    recognizer.stopListening();
}
}

Upvotes: 0

Views: 484

Answers (2)

kshubham07
kshubham07

Reputation: 391

Try the following:

  1. Log out the error code. If it is giving server error then you are not connected to the internet.

  2. If you want to use offline speech recognition, you need language models downloaded. Just verify that Speech input works on your keyboard (Press the mic button, say something and see if it is getting converted). If it is not getting recognized, then there is no error with your code, you just need to download speech models.

Upvotes: 1

Hoan Nguyen
Hoan Nguyen

Reputation: 18151

You also need

intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
                                     this.getPackageName());

Upvotes: 1

Related Questions