0

I'm trying to find out how offline voice recognition works on Jelly Bean. After some googling, I started to use SpeechRecognizer but the onResults method of the RecognitionListener always gives me the same result : "abc".

I even tried this code (from here) :

package voice.recognition.test;

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



public class voiceRecognitionTest extends Activity implements OnClickListener 
{

   private TextView mText;
   private SpeechRecognizer sr;
   private static final String TAG = "MyStt3Activity";

   @Override
   public void onCreate(Bundle savedInstanceState) 
   {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button speakButton = (Button) findViewById(R.id.btn_speak);     
        mText = (TextView) findViewById(R.id.textView1);     
        speakButton.setOnClickListener(this);
        sr = SpeechRecognizer.createSpeechRecognizer(this);       
        sr.setRecognitionListener(new listener());        
   }

   class listener implements RecognitionListener          
   {
        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);
        }
    }

    public void onClick(View v) {
        if (v.getId() == R.id.btn_speak) 
        {
            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,"voice.recognition.test");
            intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5); 
            sr.startListening(intent);
            Log.i("111111","11111111");
        }
    }
}

But it keeps giving me the same results.

I would add that I launched the code on a device (Nexus 4, OS 4.2.2).

Any suggestions on how to deal with this problem ?

Thanks!

Community
  • 1
  • 1
  • You should post some code you are trying, otherwise we can't see if you are doing something wrong. – Carlos Campderrós Jun 11 '13 at 14:59
  • I got different result running on OS 4.1.2. However, I only get only one result for each spoken word. – Hoan Nguyen Jun 12 '13 at 01:41
  • I'm running on 4.2.2 while having this problem, and I don't have another device to test on it. And I think the emulator doesn't work very well with simulating the mic... Hope someone has had the same problem and figured something out ^^ – redha_luffy Jun 12 '13 at 16:22

1 Answers1

0

I think you are extracting the results from the bundle in the wrong way. The bundle is a list of the best results, you only need the first one. Try extracting the results using this code.

public void onResults(Bundle results) {
    ArrayList<String> voiceResults = 
                results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
    if (voiceResults != null) {
       String text = voiceResults.get(0);
    }
}
tamir007
  • 178
  • 2
  • 7