15

I am looking to create an app which has Speech to text.

I am aware of this kind of ability using the RecognizerIntent: http://android-developers.blogspot.com/search/label/Speech%20Input

However - I do not want a new Intent to be popped up, I want to do the analysis a certain points in my current app, and I dont want it to pop something up stating that it is currently attempting to record your voice.

Has anybody any ideas on how best to do this. I was perhaps thinking of trying Sphinx 4 - but I dont know if this would be able to run on Android - has anyone got any advice or experience?!

I was wondering if I could alter the code here to perhaps not bothering to show the UI or button and just do the processing: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/VoiceRecognition.html

Cheers,

Stephan
  • 7,360
  • 37
  • 46
RenegadeAndy
  • 5,440
  • 18
  • 70
  • 130

4 Answers4

21

If you don't want to use the RecognizerIntent to do speech recognition, you could still use the SpeechRecognizer class to do it. However, using that class is a little bit more tricky than using the intent. As a final note, I would highly suggest to let the user know when he is recorded, otherwise he might be very set up, when he finally finds out.

Edit: A small example inspired (but changed) from, SpeechRecognizer causes ANR... I need help with Android speech API

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,
        "com.domain.app");

SpeechRecognizer recognizer = SpeechRecognizer
        .createSpeechRecognizer(this.getApplicationContext());
RecognitionListener listener = new RecognitionListener() {
    @Override
    public void onResults(Bundle results) {
        ArrayList<String> voiceResults = results
                .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
        if (voiceResults == null) {
            System.out.println("No voice results");
        } else {
            System.out.println("Printing matches: ");
            for (String match : voiceResults) {
                System.out.println(match);
            }
        }
    }

    @Override
    public void onReadyForSpeech(Bundle params) {
        System.out.println("Ready for speech");
    }

    /**
     *  ERROR_NETWORK_TIMEOUT = 1;
     *  ERROR_NETWORK = 2;
     *  ERROR_AUDIO = 3;
     *  ERROR_SERVER = 4;
     *  ERROR_CLIENT = 5;
     *  ERROR_SPEECH_TIMEOUT = 6;
     *  ERROR_NO_MATCH = 7;
     *  ERROR_RECOGNIZER_BUSY = 8;
     *  ERROR_INSUFFICIENT_PERMISSIONS = 9;
     *
     * @param error code is defined in SpeechRecognizer
     */
    @Override
    public void onError(int error) {
        System.err.println("Error listening for speech: " + error);
    }

    @Override
    public void onBeginningOfSpeech() {
        System.out.println("Speech starting");
    }

    @Override
    public void onBufferReceived(byte[] buffer) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onEndOfSpeech() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onEvent(int eventType, Bundle params) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPartialResults(Bundle partialResults) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onRmsChanged(float rmsdB) {
        // TODO Auto-generated method stub

    }
};
recognizer.setRecognitionListener(listener);
recognizer.startListening(intent);

Important: Run this code from the UI Thread, and make sure you have required permissions.

<uses-permission android:name="android.permission.RECORD_AUDIO" />
Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
Stephan
  • 7,360
  • 37
  • 46
  • Hey - Yes they will know - its going to be part of a game you see - so it will be very clear that they will be wanting to speak at those specific points. The speech recognizer class might be what I want to use - however I would love an example of its use? Got one!? – RenegadeAndy May 06 '11 at 17:46
  • 1
    I added a small example. This runs fine from the `onCreate()` method in my little test project. Don't forget to get the proper permission (RECORD_AUDIO). – Stephan May 06 '11 at 19:07
  • Fantastic - running this on my emulator returns: 05-06 20:19:38.527: ERROR/SpeechRecognizer(1745): no selected voice recognition service – RenegadeAndy May 06 '11 at 19:20
  • You probably need Google's Voice Search Application to be installed to run any speech recognition from Google. Don't know whether this is possible on the emulator. On my Nexus it ran fine. – Stephan May 06 '11 at 19:31
  • Yeah it runs fine from my phone - but would like to develop using the emulator for ease of use. I guess this means I am going to have to ask on another question probably.... – RenegadeAndy May 06 '11 at 19:41
  • This won't work in Android 11, to make it work just add this query in manifest.xml file – syed dastagir Mar 23 '21 at 08:05
6

In your activity do the following:

Image button buttonSpeak = findView....;// initialize it.
buttonSpeak.setOnClickListener(new View.OnClickListener() {

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



private void promptSpeechInput() {
    Intent intent = new 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,
            getString(R.string.speech_prompt));
    try {
        startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
    } catch (ActivityNotFoundException a) {
        Toast.makeText(getApplicationContext(),
                getString(R.string.speech_not_supported),
                Toast.LENGTH_SHORT).show();
    }
}

    @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent 
     data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
        case REQ_CODE_SPEECH_INPUT: {
            if (resultCode == RESULT_OK && null != data) {

                result = data
                        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

      EditText input ((EditText)findViewById(R.id.editTextTaskDescription));
      input.setText(result.get(0)); // set the input data to the editText alongside if want to.

            }
            break;
        }

    }
}
Saurabh Singh
  • 1,241
  • 13
  • 11
4

What is built into Android (that you launch via the intent) is a client activity that captures your voice and sends the audio to a Google server for recognition. You could build something similar. You could host sphinx yourself (or use cloud recognition services like Yapme.com), capture the voice yourself, send the audio to a recognizer, and get back text results to your app. I don't know of a way to leverage the Google recognition services without use of the Intent on Android (or through Chrome).

The general consensus I've seen so far is that today's smartphones don't really have the horsepower to do Sphinx-like speech recognition. You may want to explore running a client recognizer for yourself, but Google uses server recognition.

For some related info see:

Community
  • 1
  • 1
Michael Levy
  • 13,097
  • 15
  • 66
  • 100
0

Add Permission to MANIFEST:-

"android.permission.RECORD_AUDIO"

Call getSpeechInput() when Button is clicked (Use Listeners)

public void getSpeechInput() {

    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());

    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(intent, 10);
    } else {
        Toast.makeText(this, "Your Device Don't Support Speech Input", Toast.LENGTH_SHORT).show();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
        case 10:
            if (resultCode == RESULT_OK && data != null) {
                ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                Toast.makeText(this, result.get(0), Toast.LENGTH_SHORT).show();

            }
            break;
    }
}