Doing so makes it easy for users to use your app but it is not that easy to implement it in your app. Well, if you try it isn't that hard either. Let me show you how to add Google Speech to text to your app.
Nowadays, each and every app on the Playstore uses voice search from Google. Well, because it is much easier to just speak and have your app interpret what you say and give appropriate results instead of typing the whole thing and this makes more and more developers to add Google Speech to Text in their android app. Doing so makes it easy for users to use your app but it is not that easy to implement it in your app. Well, if you try it isn't that hard either. Let me show you how to add Google Speech to text to your app.
First create a function prompt speech input and call one intents ACTION_RECOGNIZE_SPEECH with 3 input actions viz
EXTRA_LANGUAGE_MODEL
EXTRA_LANGUAGE
EXTRA-PROMPT
[public 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(); } } ]
If the code doesn't automatically adjust press CTRL+ALT+L to reformat the code.
Now just start activity for result and get speech output.
Hope you liked the post. If you have any thoughts about the post let us know it in comments
Now just start activity for result and get speech output.
[ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data);Now you get need to declare two variables. A text view named txtSpeechInput to receive the text and one speech req code.
switch (requestCode) { case REQ_CODE_SPEECH_INPUT: { if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); txtSpeechInput.setText(result.get(0)); } break; }
} } ]
[ TextView txtSpeechInput; ImageButton speakButton; final int REQ_CODE_SPEECH_INPUT = 100; ]You are done. Now just call promptSpeechInput() whenever you want to convert speech to text. It can be on click or by setting onclicklistener to any button.
Hope you liked the post. If you have any thoughts about the post let us know it in comments
COMMENTS