2

I'm making an app that takes commands from User and write it in real time. What would be the Best option for me to take? Third Party software like sphinx or should I use the built in (android speech recognition)?

Secondly I want it to write in real time, like when I speak it starts writing?

Kara
  • 6,115
  • 16
  • 50
  • 57
Shah
  • 385
  • 1
  • 6
  • 15

2 Answers2

6

You should use the built in Android Speech recognition. Specifically, you will need to operate the SpeechRecognier API so that there is no popup dialog box.

Also, do not expect SpeechRecognizer to return anything within onPartialResults(). It rarely does.

You could try to use Sphinx, but it seems other developers have difficulty getting it to run on Android. That said, sphinx will be your only option if you want your app to run without an internet connection.

Here is a snipped of code you will need to use SpeechRecognizer:

 public void recognizeDirectly(Intent recognizerIntent)
    {
        // SpeechRecognizer requires EXTRA_CALLING_PACKAGE, so add if it's not
        // here
        if (!recognizerIntent.hasExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE))
        {
            recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
                    "com.dummy");
        }
        SpeechRecognizer recognizer = getSpeechRecognizer();
        recognizer.startListening(recognizerIntent);
    }

    @Override
    public void onResults(Bundle results)
    {
        Log.d(TAG, "full results");
        receiveResults(results);
    }

    @Override
    public void onPartialResults(Bundle partialResults)
    {
        Log.d(TAG, "partial results");
        receiveResults(partialResults);
    }

    /**
     * common method to process any results bundle from {@link SpeechRecognizer}
     */
    private void receiveResults(Bundle results)
    {
        if ((results != null)
                && results.containsKey(SpeechRecognizer.RESULTS_RECOGNITION))
        {
            List<String> heard =
                    results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
            float[] scores =
                    results.getFloatArray(SpeechRecognizer.CONFIDENCE_SCORES);
            receiveWhatWasHeard(heard, scores);
        }
    }

    @Override
    public void onError(int errorCode)
    {
        recognitionFailure(errorCode);
    }

    /**
     * stop the speech recognizer
     */
    @Override
    protected void onPause()
    {
        if (getSpeechRecognizer() != null)
        {
            getSpeechRecognizer().stopListening();
            getSpeechRecognizer().cancel();
            getSpeechRecognizer().destroy();
        }
        super.onPause();
    }

    /**
     * lazy initialize the speech recognizer
     */
    private SpeechRecognizer getSpeechRecognizer()
    {
        if (recognizer == null)
        {
            recognizer = SpeechRecognizer.createSpeechRecognizer(this);
            recognizer.setRecognitionListener(this);
        }
        return recognizer;
    }

    // other unused methods from RecognitionListener...

    @Override
    public void onReadyForSpeech(Bundle params)
    {
        Log.d(TAG, "ready for speech " + params);
    }

    @Override
    public void onEndOfSpeech()
    {
    }
gregm
  • 12,019
  • 7
  • 56
  • 78
  • Thank you sir.. But I'm not so good in android programming.. Could you please explain How to implement this code? I have implemented the normal code for speech recognition in which the dialogue appears.. How to replace it with this code ? Thanks in advance :) – Shah Mar 10 '12 at 07:15
  • Use the same Intent you did for the dialog version, but call my recognizeDirectly method instead. You'll need to implement receiveWhatWasHeard() and recognitionFailure() as well to handle the outcome of the speech recognition. – gregm Mar 10 '12 at 16:01
  • Thank you Sir.. I'm glad to hear that.. I was working in the write direction.. First task of my App is complete now.. Thanks to You :) – Shah Mar 10 '12 at 19:08
  • 1
    Sir when I Stop the app it gives mean Error.. 03-12 15:35:21.142: E/ActivityThread(30248): Activity com.yawar.Main has leaked ServiceConnection android.speech.SpeechRecognizer$Connection@40669368 that was originally bound here – Shah Mar 12 '12 at 10:45
4

gregm is right but the main "write in real time" part of the question wasn't answered. You need to add an extra to indicate that you are interested in getting parts of the result back:

Adding the extra to the intent works for me

intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);

Warning: Partial does not return only new stuff but also the previous one. So you need to implement a check for the differences by yourself...

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150