Saturday, May 2, 2015

Android speech to text Avoiding Google native Popup.

Normally, below what can be used to bring up the native speech recogniser window. 
 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,
                "Say Something!");
        try {
            ((Activity)getAppContext()).startActivityForResult(intent, 100);
        } catch (ActivityNotFoundException a) {
            Toast.makeText(getAppContext(),
                    "Sorry Speech to text is not supported",
                    Toast.LENGTH_SHORT).show();
        }

However, this above brings up the native popup. To avoid this, below technique can be used 

        speechRecognizer = SpeechRecognizer.createSpeechRecognizer(AppUtils.getAppContext());
        speechRecognizer.setRecognitionListener(new SpeechRecognizerListener());

        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,"livingmobile.com.eome");

        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5);
        speechRecognizer.startListening(intent);


public class SpeechRecognizerListener implements RecognitionListener {
    public void onResults(Bundle results)
    {
        String str = new String();
        Log.d(LOG_TAG, "onResults " + results);
        ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < data.size(); i++)
        {
            Log.d(LOG_TAG, "result " + data.get(i));
            buffer.append(data.get(i));
            buffer.append(",");

        }
        Toast.makeText(AppUtils.getAppContext(), buffer.toString(), Toast.LENGTH_SHORT).show();
    }
}

References

No comments:

Post a Comment