Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to create an SpeechToText App for Android

We will learn the steps to come with a process so as to convert the Speech into a Text in the text view for the Android devices.

With the help of this article, we will learn how we can convert speech to a textual sentence in Android making use of API.

There is a very attractive feature that Android has which lot of developers have not able to uncover yet which is the ability of the android application that makes use of the speech to text conversion feature very creatively. We all know that there is no replacement of voice commands in today’s world and we have Android providing the attribute that converts Speech into a text thereby making us to go this way. Let us see how we can convert Android’s Speech to Text API in a specific Android application.

A Look at the Sample Application:

We will have a very simple application where in there will be a button with a Mic symbol. Accessing the button there will be a trigger of Android’s Speech to Text intent that displays a dialog so as to take input as a speech which is then converted into a text you will be then able to see the text being displayed in a text view. Let us proceed step by step to reach to the final outcome.

Step 1: Creating basic Android Project

Making use of the Eclipse, we will be creating a Hello World Android project by navigating to New>Project>Android Project and give any name to the project say “Speech ToTextDemo”. Now select Android Runtime 2.1 or sdk 7. Once completed with this step, we will have a hello world Android application ready in place.

Step 2: Modifying the Layout

We will require a simple layout for our sample or demo purpose that will consist of an image button so as to trigger Speech to a text and then there will be a TextView where our results will be displayed which is converted from a speech. Now the next step would be to open layout/main.xml in the android project and its content needs to be replaced with the below code.

Listing 1: res/layout/main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView1"
    android:layout_toLeftOf="@+id/textView1"
    android:gravity="center"
    android:orientation="vertical" >
 
    <ImageButton
        android:id="@+id/btnSpeak"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:contentDescription="@string/speak"
        android:src="@android:drawable/ic_btn_speak_now" />
 
    <TextView
        android:id="@+id/txtText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:textAppearance="?android:attr/textAppearanceLarge" />
 
</LinearLayout>

The User Interface or UI will be very simple that comprise of a one Linear Layout so as to arrange the button as well as the text view. The id for button: which is btnSpeak and text view which is txtText will be used in the Java Code described below:

Step 3: Writing Java Code to trigger Speech to Text API

  • Open SpeechToTextDemoActivity class
  • Replace the code with the below mentioned.

Listing 2: SpeechToTextDemoActivity.java

package net.viralpatel.android.speechtotextdemo;
 
import java.util.ArrayList;
 
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 {
 
    protected static final int RESULT_SPEECH = 1;
 
    private ImageButton btnSpeak;
    private TextView txtText;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        txtText = (TextView) findViewById(R.id.txtText);
 
        btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);
 
        btnSpeak.setOnClickListener(new View.OnClickListener() {
 
            @Override
            public void onClick(View v) {
 
                Intent intent = new Intent(
                        RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
 
      intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
 
                try {
                    startActivityForResult(intent, RESULT_SPEECH);
                    txtText.setText("");
                } catch (ActivityNotFoundException a) {
                    Toast t = Toast.makeText(getApplicationContext(),
                   "Opps! Your device doesn't support Speech to Text",
                            Toast.LENGTH_SHORT);
                    t.show();
                }
            }
        });
 
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
 
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
 
        switch (requestCode) {
        case RESULT_SPEECH: {
            if (resultCode == RESULT_OK && null != data) {
 
                ArrayList<String> text = data
              .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
 
                txtText.setText(text.get(0));
            }
            break;
        }
 
        }
    }
}

The Speech’s to Text Android API core is the package namely android.speech and exclusively class android.speech.RecognizerIntent. Normally there is an intent android.speech.RecognizerIntent being triggered that displays dialog box so as to identify speech input. Post this, the speech is converted into a text and then results are send back to the calling Activity. One must make use of startActivityForResult() once we invoke android.speech.RecognizerIntent intent for the reason that there is a need to listen back for result text. The important thing to note here is the way we create android.speech.RecognizerIntent and trigger it and then the addition of one extra parameter using .putExtra() method.

Here we have just set the text that is obtained in the result in text view making use of txtText and utilizing txtText.setText(). We will have an exception ActivityNotFound being thrown at the time of initiation of an activity by the devices in case we have devices/android version that doesn’t support speech to text API. For the purpose of this tutorial, we have catched this exception and now a message will be displayed saying as “Opps! Your device doesn’t support Speech to Text”.

Emulator displaying the Screenshots of an Android Application

Figure 1: Emulator displaying the Screenshots of an Android Application

Emulator displaying the Screenshots of an Android Application 2

Figure 2: Emulator displaying the Screenshots of an Android Application 2

Emulator displaying the Screenshots of an Android Application 3

Figure 3: Emulator displaying the Screenshots of an Android Application 3

Emulator displaying the Screenshots of an Android Application 4

Figure 4: Emulator displaying the Screenshots of an Android Application 4

Hope you liked, see you next time.



Software developer with more than 5 years of development on Java, HTML, CSS.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login