Voice Speech to Text in Android - A Complete Guide

  • By :Subba Raju
  • Date : 24-04-2017

Do you know how Google Mic Works? Android OS provides an awesome feature called TTS i.e (Text To Speech) and STT i.e (Speech To Text). A feature available from Android 1.6 Version, android TTS allows us to provide voice as input to our application. TTS Functionalities enable an Android device to “Speak” text in Various Languages. All Android devices support TTS functionality as it is normally in-built in the TTS Engine. However, some devices lack language-specific resource files due to their limited storage capabilities.

TTS is also known as “speech synthesis”. Speech synthesis is the artificial production of human speech. It is used to translate written information into oral information, especially for mobile applications such as voice-enabled-e-mail and unified messaging.

Let us now understand the process of speech generation. First our application initiates speech generation by passing a String sequence to Synthesis framework. This framework is responsible to send the text to speech synthesis which contains executable code that manages all speech synthesis framework and core audio generated through hardware.

Let us have a look at the changes to be made in the early phase:

  • 1.Go to Settings >> Input Language or Language & Input
  • 2.Go to Google Voice typing
  • 3.Multiple Languages can be selected here
  • 4.Now go to Speech
  • 5.Now Go to Voice Input, there we see Enhanced Google service enabled
  • 6.Now go to Text-to-speech output , Preferred engine should be selected.

Input Language

 

Google Voice Typing


Language
Caption

Voice -Language

 

Voice

Here is a sample project for Voice Speech To Text.

In this project we learn - How to get Text from Voice and then how to pass the text to webpage and open link from searched text.

First, let us create a project and xml called activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layout_margin="@dimen/fifteen_dp">

  <ImageView
      android:id="@+id/speak_iv"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:layout_centerInParent="true"
      android:layout_marginBottom="@dimen/hundred_dp"
      android:contentDescription="@null"
      android:src="@drawable/ic_mic"/>

   <TextView
       android:id="@+id/text"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:textColor="@color/colorPrimary"
       android:layout_marginTop="@dimen/forty_dp"
       android:padding="@dimen/ten_dp"
       android:textSize="@dimen/sixteen_sp"
       android:text="@string/hi_hello"/>
</RelativeLayout>


 

Here we create a LinearLayout that is vertical orientation with a button (to speak) and a textview (to display the text ).

In the MainActivity we declare the id of button and textview. Define the onClickListener for button

In the onClick method

Intent intent = new Intent(
       RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

//Get the value using Intent from Speech
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");

try {
   startActivityForResult(intent, RESULT_SPEECH);
   //Set the Text as Empty
   speechText.setText("");
} catch (ActivityNotFoundException a) {
   //Show a Toast if the device is not supported
   Toast.makeText(getApplicationContext(),
           "Opps! Your device doesn't support Speech to Text",
           Toast.LENGTH_SHORT).show();
}

First We need to create RecognizerIntent by using Flags

ACTION_RECOGNIZE_SPEECH – Simply takes user’s speech input and returns it to same activity

LANGUAGE_MODEL_FREE_FORM – Considers input in free form English

We can Receive the speech response in onActivityResult which will take the appropriate action.
 

case RESULT_SPEECH: {
   if (resultCode == RESULT_OK && null != data) {

       final ArrayList<String> text = data.getStringArrayListExtra(
               RecognizerIntent.EXTRA_RESULTS);

       speechText.setText(text.get(0));

Here RecognizerIntent.EXTRA_RESULTS means An ArrayList of recognition result when performing ACTION_RECOGNIZE_SPEECH.

Generally this list should be in the descending order of speech recognizer confidence. Returned in the results not to be specified in the recognition request. Only present when RESULT_OK is returned in an activity result

The Final Code of MainActivity:

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

   private TextView speechText;
   protected static final int RESULT_SPEECH = 1;

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

       Button btnSpeak = findViewById(R.id.btn_speak);
       //Button btnRecord = findViewById(R.id.btn_record);
       speechText = findViewById(R.id.text);

       btnSpeak.setOnClickListener(this);
   }

   @Override
   public void onClick(View view) {
       switch (view.getId()) {
           case R.id.btn_speak:
               Intent intent = new Intent(
                       RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

               //Get the value using Intent from Speech
               intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");

               try {
                   startActivityForResult(intent, RESULT_SPEECH);
                   //Set the Text as Empty
                   speechText.setText("");
               } catch (ActivityNotFoundException a) {
                   //Show a Toast if the device is not supported
                   Toast.makeText(getApplicationContext(),
                           "Opps! Your device doesn't support Speech to Text",
                           Toast.LENGTH_SHORT).show();
               }
               break;
       }
   }

   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       super.onActivityResult(requestCode, resultCode, data);

       //check the requestCode as a case
       switch (requestCode) {
           case RESULT_SPEECH: {
               if (resultCode == RESULT_OK && null != data) {

                   final ArrayList<String> text = data.getStringArrayListExtra(
                           RecognizerIntent.EXTRA_RESULTS);

                   speechText.setText(text.get(0));
                   String speechValue = speechText.getText().toString();
                   if (!TextUtils.isEmpty(speechValue)) {
                       final Intent i = new Intent(this, WebActivity.class);
                       i.putExtra("SpeechValue", speechValue);
                       startActivity(i);
                   }
               }

               break;
           }

       }

   }

}

Here we get the speech text value in

String speechValue = speechText.getText().toString();

We are going to pass the value to WebActivity by using
 

i.putExtra("SpeechValue", speechValue);

After getting this value let us take this to a new activity called WebActivity

Create a xml called activity_web.xml
 

<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/activity_web_view"
   android:layout_width="match_parent"
   android:layout_height="match_parent"/>

Create id for WebView and declare in a particular activity
Now get the value from previous activity by using Bundle.
 

Bundle b = getIntent().getExtras();
if (b != null) {
   final String speechValue = b.getString("SpeechValue");
   if(!TextUtils.isEmpty(speechValue)) {
       webView.loadUrl("https://www.google.co.in/search?q="+speechValue);
   }
}

Load the url in this format: webView.loadUrl(“”)

"https://www.google.co.in/search?q="+speechValue

The value obtained from the bundle is concatenated to Google Search URL

Finally WebActivity looks like this:
 

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.WebView;

public class WebActivity extends AppCompatActivity {

   @SuppressLint("SetJavaScriptEnabled")
   @Override
   protected void onCreate(@Nullable Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_web);

       WebView webView = findViewById(R.id.activity_web_view);

       webView.getSettings().setJavaScriptEnabled(true);
       webView.getSettings().setLoadWithOverviewMode(true);
       webView.getSettings().setUseWideViewPort(true);

       Bundle b = getIntent().getExtras();
       if (b != null) {
           final String speechValue = b.getString("SpeechValue");
           if(!TextUtils.isEmpty(speechValue)) {
               webView.loadUrl("https://www.google.co.in/search?q="+speechValue);
           }
       }
   }
}
  • 1.webView.getSettings().setJavaScriptEnabled(true)
    If the web page you plan to load in your WebView uses JavaScript, you must enable JavaScript for your WebView.
  • 2.webView.getSettings().setLoadWithOverviewMode(true);
    It loads the WebView completely Zoomed Out.
  • 3.webView.getSettings().setUseWideViewPort(true);
    makes the Webview have a normal viewport.

So Now build the application and Run successfully and below screenshots for reference:
 

Input Language

Input Language

Subscribe For Our Newsletter And Stay Updated

Recent blogs

adobe aem analytics
Take a moment and think about all the different ways a potential customer interacts with your brand. He might hear about you from a...
Drupal Plan Action
Did you know, on an average an adult makes about 35000 decisions each day?! And suddenly, life feels more difficult. Most are...
Google’s DeepMind
Recent tech buzz has been all about how Google’s DeepMind has been rocking the medical and scientific world with new discoveries...

This website uses cookies to offer you an enhanced website experience. We collect information about how you interact with our website to provide personalized services to you. To find out more, see our Privacy Policy

×