Beruflich Dokumente
Kultur Dokumente
Android comes with an inbuilt feature speech to text through which you can provide
speech input to your app. With this you can add some of the cool features to your app
like adding voice navigation(Helpful when you are targeting disabled people), filling a
form with voice input etc.,
In the background how voice input works is, the speech input will be streamed to a
server, on the server voice will be converted to text and finally text will be sent back to
our app.
If you want to do the other way i.e converting text to speech, follow my previous
tutorial Android Text to Speech
DOWNLOAD CODE
I have created a simple app to demonstrate this tutorial. Below is the screenshot of the
app which contains a simple button to invoke speech input and a TextView to display the
converted speech text.
Sample Application
1. Create a new project in Eclipse by going to File New Android Application
Project and give required information.
2. Open strings.xml located under res values and add below string values.
STRINGS.XML
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Speech To Text</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="speech_prompt">Say something…</string>
<string name="speech_not_supported">Sorry! Your device doesn\'t support speech input
<string name="tap_on_mic">Tap on mic to speak</string>
</resources>
3. Open colors.xml located under res values and add below colors. If you dont see
colors.xml, create a new file and add the values.
COLORS.XML
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#ffffff</color>
<color name="bg_gradient_start">#31244e</color>
<color name="bg_gradient_end">#6b394c</color>
</resources>
4. Now open the layout file for main activity(activity_main.xml) and add below code to
create a simple layout.
ACTIVITY_MAIN.XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_gradient"
android:orientation="vertical" >
<TextView
android:id="@+id/txtSpeechInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:textColor="@color/white"
android:textSize="26dp"
android:textStyle="normal" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="60dp"
android:gravity="center"
android:orientation="vertical" >
<ImageButton
android:id="@+id/btnSpeak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:src="@drawable/ico_mic" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/tap_on_mic"
android:textColor="@color/white"
android:textSize="15dp"
android:textStyle="normal" />
</LinearLayout>
</RelativeLayout>
5. Finally open your MainActivity.java and do the following changes. In simple adding
speech input will be done in two steps.
Step
1:
Starting
RecognizerIntent
same
activity
2:
Receiving
the
speech
response
Once the speech input is done we have to catch the response in onActivityResult and
take appropriate action needed.
MAINACTIVITY.JAVA
package info.androidhive.speechtotext;
import java.util.ArrayList;
import java.util.Locale;
import
import
import
import
android.app.Activity;
android.content.ActivityNotFoundException;
android.content.Intent;
android.os.Bundle;
import
import
import
import
import
import
android.speech.RecognizerIntent;
android.view.Menu;
android.view.View;
android.widget.ImageButton;
android.widget.TextView;
android.widget.Toast;
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;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Run the app in a real device. Make sure that the device has good internet connectivity
while you are testing.
input.
Under ALL tab
select
the
language
you
want
to
download.
3. Once the language package downloaded, you can see it under INSTALLED tab.