Sie sind auf Seite 1von 21

Downloads

HOME

SPEECH INPUT

Android Speech To Text Tutorial

Android Speech To Text Tutorial

Tips

Write for Us

SEARCH HERE

Search the site

by Ravi Tamada / July 13, 2014 / 62 Comments


Android comes with an inbuilt feature speech
to text through which you can provide speech

WERE SOCIAL

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

AndroidHive
37,176 likes

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

Like Page

Contact Us

Be the first of your friends to like this

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

Subscribe to Newsletter

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.

Join our 746,498 subscribers and


get access to the latest android
tutorials, freebies, scripts and
much more!

Email

Sign Up
We strictly care about your
privacy!

POPULAR ANDROID TUTORIALS

1. Android SQLite Database


Tutorial - 1,390,004 views
2. How to connect Android with
PHP, MySQL - 1,368,219 views
3. Android JSON Parsing Tutorial 1,222,214 views
4. Android Push Notifications

using Google Cloud Messaging


(GCM), PHP and MySQL - 1,153,597
views
5. Android Sliding Menu using
Navigation Drawer - 1,054,154
views
6. Android Custom ListView with
Image and Text - 958,441 views
7. Android Login and Registration
with PHP, MySQL and SQLite 923,779 views
8. Android GPS, Location
Manager Tutorial - 702,409 views
9. Android Tab Layout with
Swipeable Views - 667,413 views
10. Android working with Google
Maps V2 - 591,588 views

So lets start by creating simple app.

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&#8230;</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>

</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_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


First we need to create a RecognizerIntent by setting necessary flags such as
AC TIO N_ R ECO GN IZ E_S PEEC H Simply takes users speech input and returns it to same
activity
LA NG U A G E_MO D EL _FR EE_FO RM Considers input in free form English
EX TRA _ PR OM PT Text prompt to show to the user when asking them to speak
Step 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 android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

private TextView txtSpeechInput;


private ImageButton btnSpeak;
private final int REQ_CODE_SPEECH_INPUT = 100;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

txtSpeechInput = (TextView) findViewById(R.id.txtSpeechInput);


btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);

// hide the action bar


getActionBar().hide();

btnSpeak.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
promptSpeechInput();
}
});

/**

/**
* Showing google speech input dialog
* */
private 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();
}
}

/**
* Receiving speech input
* */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

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.

Enabling Offline Mode


Right now all the devices are not supporting offline speech input. However you can follow this
discussion to enable offline speech input for supported devices.
I have downloaded speech input packages on my Nexus 5 and offline speech is working fine.
1. On your device go to Settings -> Language and Input. Click on icon on Google voice input.
2. Under ALL tab select the language you want to download.
3. Once the language package downloaded, you can see it under INSTALLED tab.

Subscribe to our Newsletter!


Join our 746,498 subscribers and get instant
access to the latest android tutorials, app reviews
and much more!
Email

SUBSCRIBE

ABOUT THE AUTHOR

Ravi Tamada
Ravi is hardcore Android programmer and Android programming has been his
passion since he compiled his first hello-world program. Solving real problems
of Android developers through tutorials has always been interesting part for
him.

62 Comments Android Hive


Recommend 3

Share

Login

Sort by Newest

Join the discussion

Sokhorn Houn
12 days ago

how can I get recorded voice and text at the same time?

Reply

Wiam
17 days ago

It's awesome. Thanks a lot

Lonr

Reply

Lonr

19 days ago

Question: How can i implement voice trigger input just like, "ok google" in Android's google app?

Reply

shivam

21 days ago

hi Ravi i need your help in developing some android apps i want to interconnect two apps for
sharing some details or data using mobile network not in WiFi or Bluetooth but i can't understand
how to implement can you help me for implementation.............

Reply

Ravi Tamada > shivam


21 days ago

Are the two apps installed on same device?

Reply

Adam
a month ago

thank you Mr. Ravi Tamada for this tutorial. it's helpful code,
I have one question: how I can start voice recognition directly when button clicked (without using
Intent).
do you have any Idea how I can do this?
thanks

Reply

brahmy
2 months ago

voice search miss understanding problem i am facing sir.suppose if i wana speaking R then it is

voice search miss understanding problem i am facing sir.suppose if i wana speaking R then it is
understanding like "are" and then it's displaying like this..how resolve this problem.

Reply

Arifin Firdaus
2 months ago

Hi Ravi, How can I input voice with another language other than english? Can anyone tell me???

Reply

Arifin Firdaus > Arifin Firdaus


2 months ago

i found it (change the primary input voice) in settings -> Language & input -> Voice input(under
the speech section).

Reply

Mangesh Panchwagh
2 months ago

hey
I have a question When spoken words are converted into text, how to store it
into a text file. So that I can use it(text file) later.

Reply

Rohit Kumar > Mangesh Panchwagh


2 months ago

Hi Mangesh,
I've not tried it myself but I'm pretty sure this would work. Get the text from the textView and save
it in a file. http://developer.android.com/t...

Reply

Nikhil
2 months ago

Hi Ravi, I tried implementing offline support as you have indicated. I downloaded the packages, but
still offline support is not working for my app. The app works perfectly when its online.... Any
suggestions?

Reply

Mirko Pontoriero
3 months ago

Hi, I would like to know why this App not work offline? I have a NEXUS 5 and I following the code
also the screenshot example but not work. I hope that someone answer me early. Thanks

Reply

Ravi Tamada > Mirko Pontoriero


3 months ago

Speech to Text needs internet. When we give speech to app, it connect to google services and
translates audio to text.

Reply

azad
5 months ago

i want create apk speech to text can you help me


?

Reply

Gnana poobalan
5 months ago

Does it work on Android 5 with offline mode in micromax android one device?. I need to know about

Does it work on Android 5 with offline mode in micromax android one device?. I need to know about
it. Any Suggestions would be appreciated..
Thanks in advance,
-poobalan

Reply

karuna thevar
6 months ago

" your device doesnt support speech to text ", I am getting this error

Reply

Larcio Metzner
6 months ago

You did a great Job! I'm looking for some how to get just Letters and Numbers, not full words nor
sentences. My users are going to enter information just like 'AA001', 'BC022', 'AD011', and I still had
not figure out how to get it.

Reply

Yoshua Rudy
6 months ago

Hi, Ravi I already press the subscribe button but I didn't receive any email regarding confirmation of
my subscribtion

Reply

santosh keshari
9 months ago

Hi Ravi, google voice recognition engine does not recognize medical terms. I need to implement app
to detect medical terms. Which engine shall i use? Do you have any ides?

Reply

Reply

Load more comments

Subscribe

Add Disqus to your site Add Disqus Add

Privacy

QUICK CONTACT

ABOUT ANDROIDHIVE

Advertise with us

AndroidHive is beginner's paradise for

Privacy Policy

android tutorials, tips & tricks, games,

Terms of Service

app reviews, hacks and super cool

Sitemap

advanced topics.

Copyright 2016 Droid5 Informatics Pvt Ltd.

Das könnte Ihnen auch gefallen