Android comes with the default inbuilt support for speech to text. Below are few simple steps to include this feature are:
Step 1: Create a Recognizer Intent by setting Intent flags such as 
ACTION_RECOGNIZE_SPEECH  - take user’s speech input and return to the same activity
LANGUAGE_MODEL_FREE_FORM - Consider input in free form English 
EXTRA_PROMPT - text prompt to be shown to the user when to speak. 
Step 2: Receiving the speech response
Once the speech input is done, then we have to catch the response in onActivityResult and take the appropriate action needed. 
private void promptSpeechInput
{
 Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
 intnet.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 ex)
 {
  ex.printStackTrace();
 }
}
protected void onActivityResult(int requestedCode, int resultCode, Intent data)
{
 super.onActivityResult(requestedCode, resultCode, data);
 switch(requestCode)
 {
  case REQ_CODE_SPEECH_INPUT:
  {
   if (resultCode == RESULT_OK && data != null)
   {
    ArrayLis result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS).txtSpeechInput.setText(result.get(0)); 
    break;
   }
  }
 }
}
References:
 
No comments:
Post a Comment