Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use speech recognition without the annoying dialog in android phones

People also ask

What is offline speech recognition on Android?

Android. Speech recognition can be activated when typing on your Android device. If this facility is available in the app you are using, a microphone icon will appear on the keypad. Pressing this activates the speech recognition. Android does have offline speech recognition capabilities.


Use the SpeechRecognizer interface. Your app needs to have the RECORD_AUDIO permission, and you can then create a SpeechRecognizer, give it a RecognitionListener and then call its startListening method. You will get callbacks to the listener when the speech recognizer is ready to begin listening for speech and as it receives speech and converts it to text.


GAST has a handy abstract class you can use to use the SpeechRecognizer class with very little new code. There is also an example of running the SpeechRecognizer as a background service using this and this


Thanks for posting this! I found it helpful to define the onclick listener in oncreate:

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

    mText = (TextView) findViewById(R.id.textView1);     
    MyRecognitionListener listener = new MyRecognitionListener();
    sr = SpeechRecognizer.createSpeechRecognizer(this);       
    sr.setRecognitionListener(listener);

    findViewById(R.id.button1).setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) 
        {
                Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);    
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
                intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,1); 
                intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test");
                sr.startListening(intent);
        }
    });     
}

I end up making Github project to convert Text to speech and speech to text without annoying dialog,

https://github.com/hiteshsahu/Android-TTS-STT/tree/master/app/src/main/java/com/hiteshsahu/stt_tts/translation_engine

 //SPEECH TO TEXT DEMO
    speechToText.setOnClickListener({ view ->

        Snackbar.make(view, "Speak now, App is listening", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show()

        TranslatorFactory
                .instance
                .with(TranslatorFactory.TRANSLATORS.SPEECH_TO_TEXT,
                        object : ConversionCallback {
                            override fun onSuccess(result: String) {
                                sttOutput.text = result
                            }

                            override fun onCompletion() {
                            }

                            override fun onErrorOccurred(errorMessage: String) {
                                erroConsole.text = "Speech2Text Error: $errorMessage"
                            }

                        }).initialize("Speak Now !!", this@HomeActivity)

    })


    //TEXT TO SPEECH DEMO
    textToSpeech.setOnClickListener({ view ->

        val stringToSpeak :String = ttsInput.text.toString()

        if (null!=stringToSpeak &&  stringToSpeak.isNotEmpty()) {

            TranslatorFactory
                    .instance
                    .with(TranslatorFactory.TRANSLATORS.TEXT_TO_SPEECH,
                            object : ConversionCallback {
                                override fun onSuccess(result: String) {
                                }

                                override fun onCompletion() {
                                }

                                override fun onErrorOccurred(errorMessage: String) {
                                    erroConsole.text = "Text2Speech Error: $errorMessage"
                                }

                            })
                    .initialize(stringToSpeak, this)

        } else {
            ttsInput.setText("Invalid input")
            Snackbar.make(view, "Please enter some text to speak", Snackbar.LENGTH_LONG).show()
        }

    })

enter image description here