11

I am trying to add a button in my application that starts Google Voice Typing (or the default speech recognition). I have tried following this tutorial. This tutorial is incredibly confusing to me. I imported the .jar, and added the necessary permissions, services, and activities to my Manifest. But I can't seem to figure out how to "put it all together". I'm wondering:

  1. Am I supposed to call the inputMethodService from my button click in my Main Activity? Or does my inputMethodService essentially become my Main Activity?
  2. What does IME mean? I tried to Google it, but the definitions it gave me didn't help my understanding.
  3. When I try to copy and paste the whole DemoInputMethodService code into my current activity, I get an error saying I cannot extend InputMethodService inside of this activity. (Which leads back to to ask question one.)

How can I get this to work?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Ethan
  • 175
  • 2
  • 11
  • just do a layout for text input area. when the IME keyboard comes up, if you have configured your phone for "languange and input // speech // voice input" then the keyboard will include a microphone icon. tap that.... otherwise you can use SpeechRecognizer Intent. – Robert Rowntree Feb 09 '15 at 16:02
  • I was trying to get away from doing that. In my case, using a button that automatically starts Google Voice Typing would be a much better option, improving both appearance and ease of use. When using a SpeechRecognizer Intent will my phone open up this Google feature? – Ethan Feb 09 '15 at 16:57
  • 1
    Nice name! Anyways, in terms of Android an IME is an Input Method Editor. – Ethan Feb 09 '15 at 17:23
  • Hey thanks @Ethan, you too! But does anyone know how I can start Google-Voice-Typing with a button? – Ethan Feb 09 '15 at 18:14
  • You can implement through this demo app https://github.com/Ram8948/google-voice-typing-integration/ – Ram Apr 04 '16 at 12:05

4 Answers4

4

If you want to follow the tutorial that you mention then you need to implement an IME (input method editor) first, see http://developer.android.com/guide/topics/text/creating-input-method.html

This IME can have a regular keyboard look-and-feel or contain just a microphone button.

The user of your app will first have to click on a text field to launch the IME. (Note that there can be several IMEs installed on the device and they have to be explicitly enabled in the Settings.) Then the user will have to click on the microphone button to trigger the speech recognition.

The tutorial provides a jar that lets you directly call Google's recognizer. It would be nicer if instead you called the recognizer via the SpeechRecognizer-interface (http://developer.android.com/reference/android/speech/SpeechRecognizer.html), this way the user can decide whether to use Google's or something else.

The SpeechRecognizer is given a listener which supports the method onPartialResults, which allows you to monitor the recognition hypotheses while the user is speaking. It's up to you how you display them. Note however that the specification of SpeechRecognizer does not promise that this method gets called. This depends on the implementation of the recognizer service. Regarding Google's implementation: what it supports keeps changing unannounced, it does not have a public API nor even release notes.

You might be able to reuse my project Kõnele (http://kaljurand.github.io/K6nele/about/), which contains two implementations of SpeechRecognizer and an IME that uses them. One of the implementations offers continuous recognition of arbitrarily long audio input, using the Kaldi GStreamer server (https://github.com/alumae/kaldi-gstreamer-server). You would need to set up your own instance of the server porting it to the language that you want to recognize (unless you want to use the Estonian server that Kõnele uses by default).

Kaarel
  • 10,554
  • 4
  • 56
  • 78
  • Ok, so IME is not the way to go for me. I'll check out the ApeechRecognizer-Interface right now. – Ethan Feb 09 '15 at 21:11
  • If I use this interface will the SpeechRecognizer display the text as it recognizes it? Similar to Google's Speech Typing? Or will it record for a few seconds and then shut off? – Ethan Feb 09 '15 at 21:14
0

Voice recognition samples are found where you have the android SDK..

example:

$ find $SDK_ROOT/samples -name *recogni*


./android-19/legacy/VoiceRecognitionService/res/xml/recognizer.xml
./android-19/legacy/VoiceRecognitionService/src/com/example/android/voicerecognitionservice
./android-19/legacy/ApiDemos/res/layout/voice_recognition.xml
./android-18/legacy/VoiceRecognitionService/res/xml/recognizer.xml
./android-18/legacy/VoiceRecognitionService/src/com/example/android/voicerecognitionservice
./android-18/legacy/ApiDemos/res/layout/voice_recognition.xml
./android-21/legacy/VoiceRecognitionService/res/xml/recognizer.xml
./android-21/legacy/VoiceRecognitionService/src/com/example/android/voicerecognitionservice
./android-21/legacy/ApiDemos/res/layout/voice_recognition.xml

any one of the services should help show how to do a RecognizerIntent

The "APIDemo" seems to include use of a RecognizerIntent. check the source for that one. Otherwise look into the services and carve them up into an intent.

Robert Rowntree
  • 6,230
  • 2
  • 24
  • 43
  • Does a RecognizerIntent mean it will open and start Google-Voice-Typing? Or will it open a voice recognizer that will only record for a few seconds and then stop? – Ethan Feb 09 '15 at 18:16
0

I had the same issue, but after a long time looking for continuous voice dictation on an activity, I solved that problem using pocketsphinx.

I couldn't find the way to integrate Google Voice Typing on an activity, just on an input method by following that tutorial. If it confuse you, just download this demo and modify it. Good Luck!

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Nacho Silva
  • 240
  • 1
  • 6
  • I never found the answer. The decision I made was that most phones that would run my app would have some sort of voice recognition built-in. – Ethan Feb 27 '15 at 20:21
0

You can trigger an intent from a button listener

Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);

And the result can be get from

private TextToSpeech mTts;
protected void onActivityResult(
        int requestCode, int resultCode, Intent data) {
    if (requestCode == MY_DATA_CHECK_CODE) {
        if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
            // success, create the TTS instance
            mTts = new TextToSpeech(this, this);
        } else {
            // missing data, install it
            Intent installIntent = new Intent();
            installIntent.setAction(
                TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            startActivity(installIntent);
        }
    }
}

Refer this link for more info.

Kiran Benny Joseph
  • 6,755
  • 4
  • 38
  • 57