Not getting results from SpeechRecognizer class?

I have a maind UI class that has button which instantiate a class that is implementing SpeechRecognizer library to convert speech from text.

btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);

        btnSpeak.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                startNoiseProcessService();
            }
        });

startNoiseProcessService() is a function which is calling another class object.

public void startNoiseProcessService() {

                StreamService ss = new StreamService(this);
                String s = ss.startStreaming();
                if ( s.equals("NA") ) {
                    Toast t = Toast.makeText(getApplicationContext(),threadCnt + 
                            "Opps! Your device doesn't support Speech to Text",
                            Toast.LENGTH_SHORT);
                    t.show();
            }

}

StreamService class implements SpeechRecognizer API.

This StreamService class is getting called perfectly fine and problem lies is I am unable to get the converted text from speech at the time after results are calculated by onResults(Bundle) method.

I need the converted text to update my textbox and I am not getting it.

startStreaming() implementation:

 public String startStreaming() {

            text = "";

            if ( !SpeechRecognizer.isRecognitionAvailable(context) ) {
                text = "NA";
                return text;
            }

            Log.d(TAG, "started taking input");
            sr = SpeechRecognizer.createSpeechRecognizer(context);

            Intent intent = new Intent(
                     RecognizerIntent.ACTION_RECOGNIZE_SPEECH);


            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
            intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);

            sr.setRecognitionListener( new mylistener());
            sr.startListening(intent);

     }

Answers


Based on your comment, I think you can't have the results at the time sr.startListening(intent) is called. You need to get the results using the onResults method of the listener and process the text that comes inside the Bundle from there.

The way I implemented this was to have a Service in charge of handling the SpeechRecognizer. Then when onResults is called I send the text back to the original caller using a message handler. You can see details on how to do this here: https://developer.android.com/training/multiple-threads/communicate-ui.html


Need Your Help

Floating divs left, make all divs height equal the tallest div in it's row?

html css

I am attempting a design that would float a series of divs, all the same class, to the left. I want the divs to fit into rows, where in each row the divs are the same height so that there is no bre...

Zend Studio with Remote Host option doesn't work in VPN networks & Android USB tethering mode

android ftp zend-studio filezilla tethering

I'm using Zend Studio with Remote Host option (FTP) for working & testing directly in hosting. However, when I connect internet with my university's network, Zend Studio doesn't upload the code...