Android has a very cool feature that still many developers dont know.
Apps like Any.DO uses speech to text conversion feature quite
creatively. In today’s world of Siri, voice commands are of utmost
importance. Android natively provides feature of Speech to Text so why
not to use it in our app!
I will show you how to use Android’s Speech to Text API in an application.
Let’s make our demo application.
Once you are done with above steps, you will have a basic hello world Android App.
Open layout/main.xml in your android project and replace its content with following:
File: res/layout/main.xml
The UI is very simply. One LinearLayout to organize the button and text view. Note the id for button:
File: SpeechToTextDemoActivity.java
The heart of Speech to text Android API is package
Note how in above code we crate intent android.speech.RecognizerIntent and trigger it. Also we add one extra parameter using
Since we triggered the RecognizerIntent via startActivityForResult(), we override method
RecognizerIntent will convert the speech input to text and send back the result as ArraList with key RecognizerIntent.EXTRA_RESULTS. Generally this list should be ordered in descending order of speech recognizer confidence. Only present when RESULT_OK is returned in an activity result. We just set the text that we got in result in text view
One thing worth noting here is how to handle devices/android version that doesn’t support speech to text API. In such case, exception ActivityNotFoundException will be thrown when we try to start activity. In above example, we have catched this exception and displayed a message “Opps! Your device doesn’t support Speech to Text” using Toast.
I will show you how to use Android’s Speech to Text API in an application.
Let’s make our demo application.
Demo App
The App will be very simple. It will have a button with Mic symbol. On click of which we trigger Android’s Speech to Text Intent which shows a dialog to take speech input. The speech input is then converted into text. The text is then displayed in a text view.Step 1: Create Basic Android Project in Eclipse
Create a Hello World Android project in Eclipse. Go to New > Project > Android Project. Give the project name as SpeechToTextDemo and select Android Runtime 2.1 or sdk 7. I have given package namenet.viralpatel.android.speechtotextdemo
.Once you are done with above steps, you will have a basic hello world Android App.
Step 2: Change the Layout
For our demo, we need simple layout. Just one Image Button to trigger Speech to Text API and one TextView to display result text that is converted from speech.Open layout/main.xml in your android project and replace its content with following:
File: 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 > |
btnSpeak
and text view: txtText
which we will use in our Java code.Step 3: Android Java Code to trigger Speech to Text API
Open SpeechToTextDemoActivity class and replace the code with following.File: 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 ; } } } } |
android.speech
and specifically class android.speech.RecognizerIntent
.
Basically we trigger an Intent (android.speech.RecognizerIntent) which
shows dialog box to recognize speech input. This Activity then converts
the speech into text and send backs the result to our calling Activity.
When we invoke android.speech.RecognizerIntent intent, we must use startActivityForResult()
as we must listen back for result text.Note how in above code we crate intent android.speech.RecognizerIntent and trigger it. Also we add one extra parameter using
.putExtra()
method. When invoking RecognizerIntent, we must provide extra RecognizerIntent.EXTRA_LANGUAGE_MODE. Here we are setting its value to en-US.Since we triggered the RecognizerIntent via startActivityForResult(), we override method
onActivityResult(int requestCode, int resultCode, Intent data)
to handle the result data. TheRecognizerIntent will convert the speech input to text and send back the result as ArraList with key RecognizerIntent.EXTRA_RESULTS. Generally this list should be ordered in descending order of speech recognizer confidence. Only present when RESULT_OK is returned in an activity result. We just set the text that we got in result in text view
txtText
using txtText.setText()
.One thing worth noting here is how to handle devices/android version that doesn’t support speech to text API. In such case, exception ActivityNotFoundException will be thrown when we try to start activity. In above example, we have catched this exception and displayed a message “Opps! Your device doesn’t support Speech to Text” using Toast.
No comments:
Post a Comment