[Android] The speech recognition

In this lesson, you will learn to use the speech recognition.

For this, you will use the Intent ACTION_RECOGNIZE_SPEECH.

To use the speech recognition system, use the Intent ACTION_RECOGNIZE_SPEECH.

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Talk now !");
try {
  startActivityForResult(intent, 123);
} catch (ActivityNotFoundException e) {
  // Logiciel de reconnaissance vocale non installé
}

Once the engine has analyzed the sound, it sends the result by intent :

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if(requestCode == 123) && resultCode == Activity.RESULT_OK) {
      // Analyse du résultat
    }
  }
}

If the software of speech recognition can’t be find, it can be installed automatically :

Intent intentInstall = new Intent(
  Intent.ACTION_VIEW, 
  Uri.parse("market://details?id=com.google.android.voicesearch")
);

In the method onActivityResult, the result of the analysis is in the argument data as ArrayList of String :

ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String text = "";
for (String result : results) {
  text += result + "\\n";
}
et_output.setText (text);

Layout main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <EditText
      android:id="@+id/et_output"
      android:layout_width="fill_parent"
      android:layout_height="match_parent"
      android:text="Enter a value" />
</LinearLayout>

File Main.java

public class Main extends Activity {
       
  private static final int REQUEST_CODE = 1;
  EditText et_output;
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    et_output = (EditText)findViewById (R.id.et_output);
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Talk now !");
    try {
      startActivityForResult(intent, REQUEST_CODE);
    } catch (ActivityNotFoundException e) {
      Toast.makeText(this, "No recognition software installed. Download it.", Toast.LENGTH_SHORT).show();
      Intent intentInstall = new Intent(
        Intent.ACTION_VIEW, 
        Uri.parse("market://details?id=com.google.android.voicesearch")
      );
    }
  }
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == REQUEST_CODE) {
      if (resultCode == Activity.RESULT_OK) {
        ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        String text = "";
        for (String result : results) {
          text += result + "\\n";
        }
        et_output.setText (text);
      } else {
        Toast.makeText(this, "Operation failed", Toast.LENGTH_SHORT).show();
      }
      }
  }
}

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×