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 tutorial Android Text to Speech
In this tutorial, we are going to learn how to use convert Speech/Voice to Text.
1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. By default my activity is MainActivity.java.
2. First create a layout in xml file naming activity_main.xml that contains Button for starting voice recognition and a ListView to display the result.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="15dp" android:text="@string/click_text" android:textColor="#0587d9" android:textSize="18sp" android:textStyle="bold" /> <Button android:id="@+id/speakButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:padding="5dp" android:text="@string/click" /> <ListView android:id="@+id/text_listview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_margin="5dp" /> </LinearLayout> |
3. Now open your MainActivity.java and add the following lines. There are only two steps by which we can achieve our goal :
Step 1: Starting RecognizerIntent
First we need to create a RecognizerIntent by setting necessary flags such as :
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.
Note : Before doing anything we have to check if the device support Speech Reocgnizer or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
package com.voice_to_text; import java.util.ArrayList; import java.util.List; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.os.Bundle; import android.speech.RecognizerIntent; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; public class MainActivity extends AppCompatActivity { private static final int REQUEST_CODE = 1234; private static ListView wordsList; private static Button speakButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { speakButton = (Button) findViewById(R.id.speakButton); wordsList = (ListView) findViewById(R.id.text_listview); // Disable button if no recognition service is present PackageManager pm = getPackageManager(); List<ResolveInfo> activities = pm.queryIntentActivities(new Intent( RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0); if (activities.size() == 0) { speakButton.setEnabled(false); speakButton.setText(R.string.recognizer); } speakButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { startVoiceRecognitionActivity(); } }); } /** * Fire an intent to start the voice recognition activity. */ private void startVoiceRecognitionActivity() { Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Voice Recognition Demo..."); startActivityForResult(intent, REQUEST_CODE); } /** * Handle the results from the voice recognition activity. */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) { // Populate the wordsList with the String values the recognition // engine thought it heard ArrayList<String> matches = data .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); wordsList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, matches)); } super.onActivityResult(requestCode, resultCode, data); } } |
4. Now, you are all done, run your app and you will get the output as shown in video.
Thanks.
Subscribe to us and get the latest news.
1 Comment
cabot
Sunday, August 26th, 2018Thanks for this helpful tutorial it was very detailed and to the point.
Here is a related video you can take a look at it to see how you can turn text to video: https://youtu.be/L94717pI14M