Question

I'm trying to program an app that allows me to say the commands and have the app perform what i said. But here's the problem, i don't want to use the RecognizerIntent that have the GoogleVoice pop up. I want to have my own customized one. Can anyone give me some help or hints that will allow me to do that? And maybe some help on how to use the results after i said something and perform the task?

Updated code:

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

public class VoicingMain extends Activity implements OnClickListener {

ListView lv;    
private SpeechRecognizer sr;
private Intent srIntent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.voicing);
    lv = (ListView) findViewById(R.id.lvList);
    Button b = (Button) findViewById(R.id.bStartVoicing);
    b.setOnClickListener(this);
    boolean available = SpeechRecognizer.isRecognitionAvailable(this);
    Log.d("Speech", "available = " + available);
    sr = SpeechRecognizer.createSpeechRecognizer(this);
    sr.setRecognitionListener(new SpeechListener());

}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    srIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    srIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    srIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
            this.getPackageName());
    srIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);
    Log.d("speech", "button active");
    sr.startListening(srIntent);
    new CountDownTimer(3000, 1000) {

        @Override
        public void onTick(long arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onFinish() {
            // TODO Auto-generated method stub
            sr.stopListening();
        }

    }.start();
}

private class SpeechListener implements RecognitionListener {

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

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

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

    @Override
    public void onError(int arg0) {
        // TODO Auto-generated method stub
        Log.d("Speech", "onError");
    }

    @Override
    public void onEvent(int arg0, Bundle arg1) {
        // TODO Auto-generated method stub
        Log.d("Speech", "onEvent");
    }

    @Override
    public void onPartialResults(Bundle arg0) {
        // TODO Auto-generated method stub
        Log.d("Speech", "onPartialResults");
    }

    @Override
    public void onReadyForSpeech(Bundle arg0) {
        // TODO Auto-generated method stub
        Log.d("Speech", "onReadyForSpeech");
    }

    @Override
    public void onResults(Bundle arg0) {
        // TODO Auto-generated method stub
        Log.d("Speech", "results");
    }

    @Override
    public void onRmsChanged(float arg0) {
        // TODO Auto-generated method stub
        // Log.d("Speech", "onRmsChanged");
    }

}

}

This is what i have so far

Was it helpful?

Solution

This SO question shows how to get access to the Results in your SpeechListener. These results are available in the Results() method.

As for the ability to perform tasks, you should have some kind of handler that will start the necessary services depending on the text. This handler could use intents to communicate with the services. For example, here is how you could react to the user saying "Call Bob" (based off link above):

        @Override
        public void onResults(Bundle results) {
            ArrayList<String> voiceResults = results
                    .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
            if (voiceResults == null) {
                Log.e(TAG, "No voice results");
            } else {
                for (int i = 0; i < voiceResults.size(); ++i) {
                    if( voiceResults[i].contains("Call") )
                      for( int j = i+1; j < matches.size(); ++j )
                        if( voiceResults[j].contains("Bob") )
                        {
                          //Get Bob's phone number, using [this][2]
                          String bobNumber = "123 123 1234";
                          Intent callIntent = new Intent(Intent.ACTION_CALL);          
                          callIntent.setData(Uri.parse("tel:"+bobNumber));          
                          startActivity(callIntent);
                          break;
                        }
                }
            }
        }

OTHER TIPS

You can Simply Put a SetOnClickListener on any button. inside activity or fragment than start onACtivityResult with result code compared with other values.

take a look at code.

OnClickListener

                R.id.searchVoice -> {
                val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
                intent.putExtra(
                    RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                    RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
                )
                intent.putExtra(
                    RecognizerIntent.EXTRA_LANGUAGE,
                    Locale.getDefault());
                intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak to text")

                try {
                    startActivityForResult(intent, REQUEST_CODE_SPEECH_INPUT)
                } catch (e: Exception) {
                    Toast.makeText(
                        mContext, " " + e.message,
                        Toast.LENGTH_SHORT
                    )
                        .show()
                }
            }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)


        if (requestCode == REQUEST_CODE_SPEECH_INPUT) {
            if (resultCode == RESULT_OK && data != null) {
                val result = data.getStringArrayListExtra(
                    RecognizerIntent.EXTRA_RESULTS
                )
                if (result != null) {
                    if(result.contains("search products")){
                        val intent = Intent(this, ActivitySpecificProduct::class.java)
                        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
                        startActivity(intent)
                    }
                    else  if(result.contains("update products")){
                        val intent = Intent(this, ActivitySpecificProduct::class.java)
                        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
                        startActivity(intent)
                    }
                    else  if(result.contains("add products")){
                        val intent = Intent(this, ActivityStoreQR::class.java)
                        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
                        startActivity(intent)
                    }
                    else Toast.makeText(this, "Try something else", Toast.LENGTH_LONG).show()
                }
            }
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top