Usually, one can speak more than 100 words per minute but cannot type more than 50 words per minute. Android brings in the power of the speech to text feature, built into our smart phones. There are numerous possibilities with the speech recognition function built into our devices and a number of apps are utilizing this, now a days.
In this tutorial, let us see, how to implement the speech to text function in our project.
Start a new Android Project.
Next, open the (default layout) activity_main.xml and place an ImageView and a TextView.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:weightSum="8"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1"></LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/textView" android:gravity="center" android:weightSum="1"> <TextView android:id="@+id/txt_voice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textCapSentences" android:textColor="#000000" android:textSize="32dp" android:textStyle="normal" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="5"></LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="2.5" android:gravity="center"> <ImageView android:id="@+id/img_mic" android:layout_width="wrap_content" android:layout_height="71dp" android:layout_weight="0.05" android:background="#ffffff" android:src="@drawable/mic" /> </LinearLayout> </LinearLayout>
When we touch the mic icon, the Google voice input window will be activated and we can speak into the device. The output will be shown in the TextView. In the MainActivity java class, we will write the code to convert the speech to text and display it in the TextView. The speech is recognized by the RecognizerIntent.ACTION_RECOGNIZE_SPEECH function.
import android.content.ActivityNotFoundException; import android.content.Intent; import android.speech.RecognizerIntent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import java.util.ArrayList; import java.util.Locale; public class MainActivity extends AppCompatActivity { TextView textVoice; ImageView textSpeak; private final int SPEECH_INPUT = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //initialize textview and imageview textVoice = (TextView) findViewById(R.id.txt_voice); textSpeak = (ImageView) findViewById(R.id.img_mic); textSpeak.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { askSpeechInput(); } }); } // Showing google speech input dialog private void askSpeechInput() { Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault()); i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Listening..!"); try { startActivityForResult(i, SPEECH_INPUT); } catch (ActivityNotFoundException a) { } } // Receiving speech input @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case SPEECH_INPUT: { if (resultCode == RESULT_OK && null != data) { ArrayListresult = data .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); //arraylist result contains text that are similar to what you said //the first one would be more accurate,so set it to textView textVoice.setText(result.get(0)); } break; } } } }