Академический Документы
Профессиональный Документы
Культура Документы
Dismiss
ANDROID SDK
5 1 18
The Android platform provides support for both speech recognition and speech
synthesis. In this tutorial, we will create a simple Android app which allows the user
to speak, attempts to recognize what they say, and then repeats what was
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
recognized back to them using the Text To Speech engine.
We will use the same technique for the TTS engine as we did inAndroid SDK:
Using the Text to Speech Engine, so the focus of this tutorial will be on the speech
recognition element. Both speech recognition and synthesis are relatively easy to
implement on the Android platform, so you should be able to achieve the steps in
this tutorial even if you are an Android beginner.
The app is going to use a few text Strings as part of the interface, so define them
by opening the "res/values/strings.xml" file and entering the following content:
1 <resources>
2 <string name="intro">Press the button to speak!</
string>
3 <string name="app_name">SpeechRepeat</string>
4 <string name="speech">Speak now!</string>
5 <string name="word_intro">Suggested words…</
string>
6 </resources>
Of course, you can alter the String content in any way you like.
Open your "res/layout/main.xml" file to create the main app layout. Switch to the
XML editor if the graphical editor is displayed by default. Enter a Linear Layout as
the main layout for the app's launch Activity:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
2 android:layout_width="fill_parent"
3 android:layout_height="fill_parent"
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
3 android:layout_height="fill_parent"
4 android:orientation="vertical"
5 android:background
="#ff330066"
6 android:paddingBottom="5dp" >
7
8 </LinearLayout>
The Linear Layout contains various style declarations including a background color.
Inside the Linear Layout, first enter an informative Text View:
1 <TextView android:layout_width="fill_parent"
2 android:layout_height="wrap_content"
3 android:text="@string/intro"
4 android:padding="5dp"
5 android:textStyle="bold"
6 android:textSize="16dp"
7 android:gravity="center"
8 android:textColor="#ffffff33" />
Notice that the Text View refers to one of the Strings we defined. It also sets various
display properties which you can alter if you wish. After the Text View, add a button:
1 <Button android:id="@+id/speech_btn"
2 android:layout_width="match_parent"
3 android:layout_height="wrap_content"
4 android:text="@string/speech" />
The user will press this button in order to speak. We give the button an ID so that
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
we can identify it in the Java code and display one of the Strings we defined on it.
After the button, add another informative Text View, which will precede the list of
suggested words:
1 <TextView android:layout_width="fill_parent"
2 android:layout_height="wrap_content"
3 android:padding="5dp"
4 android:text="@string/word_intro"
5 android:textStyle="italic" />
Again, this Text View uses a String resource and contains style properties. The last
item in our main.xml Linear Layout is the list of suggested words:
01 <ListView android:id="@+id/word_list"
02 android:layout_width="fill_parent"
03 android:layout_height="0dip"
04 android:layout_weight="1"
05 android:paddingLeft="10dp"
06 android:paddingTop="3dp"
07 android:paddingRight="10dp"
08 android:paddingBottom="3dp"
09 android:layout_marginLeft="20dp"
10 android:layout_marginRight="20dp"
11 android:layout_marginTop="5dp"
12 android:layout_marginBottom="5dp"
13 android:background="@drawable/words_bg"/>
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
The List View will be populated with data when the app runs, so we give it an ID for
identification in Java. The element also refers to a drawable resource, which you
should add to each of the drawables folders in your app's "res" directory, saving it
as "words_bg.xml" and entering the following content:
01 <shape xmlns:android="http://schemas.android.com/apk/res/android
"
02 android:dither="true">
03 <gradient
04 android:startColor="#ff000000"
05 android:endColor="#ff000000"
06 android:centerColor="#00000000"
07 android:angle="180" />
08 <corners android:radius="10dp" />
09 <stroke
10 android:width="2dp"
11 android:color="#66ffffff" />
12 </shape>
This is a simple shape drawable to display behind the List View. You can of course
alter this and the List View style properties if you wish. The only remaining user
interface item we need to define now is the layout for a single item within the list,
each of which will display a word suggestion. Create a new file in "res/layout"
named "word.xml"and enter the following code:
1 <TextView xmlns:android="http://schemas.android.com/apk/res/android
"
2 android:layout_width="fill_parent"
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
2 android:layout_width="fill_parent"
3 android:layout_height="fill_parent"
4 android:gravity="center"
5 android:padding="5dp"
6 android:textColor="#ffffffff"
7 android:textSize="16dp" >
8 </TextView>
Each item in the list will be a simple Text View. That's our interface design complete.
This is how the app appears on initial launch:
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Note: don't worry about the lack of dithering, this is just how it looks in the
DDMS screenshot. On the device itself, the gradient is perfectly smooth.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Step 3: Setup Speech Recognition
Now we can implement our Java code. Open your app's main Activity and add the
following import statements at the top:
01 import java.util.ArrayList;
02 import java.util.List;
03 import java.util.Locale;
04
05 import android.app.Activity;
06 import android.content.Intent;
07 import android.content.pm.PackageManager;
08 import android.content.pm.ResolveInfo;
09 import android.os.Bundle;
10 import android.speech.RecognizerIntent;
11 import android.speech.tts.TextToSpeech.OnInitListener;
12 import android.speech.tts.TextToSpeech;
13 import android.util.Log;
14 import android.view.View;
15 import android.view.View.OnClickListener;
16 import android.widget.AdapterView;
17 import android.widget.AdapterView.OnItemClickListener;
18 import android.widget.ArrayAdapter;
19 import android.widget.Button;
20 import android.widget.ListView;
21 import android.widget.Toast;
22 import android.widget.TextView;
You may not need all of these if you do not implement the TTS functionality -
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Eclipse should highlight imports you have not used so check them when you finish
coding. Extend your opening class declaration line as follows, altering the Activity
name to suit your own:
The "OnInitListener" is only required for the TTS function. Add the following instance
variables inside your class declaration, before the "onCreate" method:
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Inside your "onCreate" method, your class should already be calling the superclass
method and setting your main layout. If not, it should begin like this:
1 //call superclass
2 super.onCreate(savedInstanceState);
3 //set content view
4 setContentView(R.layout.main);
Next, still inside your "onCreate" method, retrieve a reference to the speech button
and list we created, using their ID values:
The List View is an instance variable, accessible throughout the class. Now we
need to find out whether the user device has speech recognition support:
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
06 speechBtn.setOnClickListener(
this);
07 }
08 else
09 {
10 //speech recognition not supported, disable button and output message
11 speechBtn.setEnabled(false);
12 Toast.makeText(this, "Oops - Speech recognition not supported!"
, Toast.LENGTH_LON
13 }
1 /**
2 * Called when the user presses the speak button
3 */
4 public void onClick(View v) {
5 if (v.getId() == R.id.speech_btn) {
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
5 if (v.getId() == R.id.speech_btn) {
6 //listen for results
7 listenToSpeech();
8 }
9 }
Now implement the method we've called here after the "onClick" method:
01 /**
02 * Instruct the app to listen for user speech input
03 */
04 private void listenToSpeech() {
05
06 //start the speech recognition intent passing required data
07 Intent listenIntent =new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
08 //indicate package
09 listenIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPa
10 //message to display while listening
11 listenIntent.putExtra(RecognizerIntent.EXTRA_PROMPT,
"Say a word!"
12 //set speech model
13 listenIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.
14 //specify number of results to retrieve
15 listenIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,
10);
16
17 //start listening
18 startActivityForResult(listenIntent, VR_REQUEST);
19 }
Some of this code is standard for setting up the speech recognition listening
functionality. Areas to pay particular attention to include the line in which we specify
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
the "EXTRA_PROMPT" - you can alter this to include text you want to appear for
prompting the user to speak. Also notice the "EXTRA_MAX_RESULTS" line, in
which we specify how many suggestions we want the recognizer to return when the
user speaks. Since we are calling the "startActivityForResult" method, we will
handle the recognizer results in the "onActivityResult" method.
When the app is listening for user speech, it will appear as follows:
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Step 5: Present Word Suggestions
Implement the "onActivityResult" method inside your class declaration as follows:
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Implement the "onActivityResult" method inside your class declaration as follows:
01 /**
02 * onActivityResults handles:
03 * - retrieving results of speech recognition listening
04 * - retrieving result of TTS data check
05 */
06 @Override
07 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
08 //check speech recognition result
09 if (requestCode == VR_REQUEST && resultCode == RESULT_OK)
10 {
11 //store the returned word list as an ArrayList
12 ArrayList<String> suggestedWords = data.getStringArrayListExtra(Recognizer
13 //set the retrieved list to display in the ListView using an ArrayAdapter
14 wordList.setAdapter(new ArrayAdapter<String> t(his, R.layout.word, suggested
15 }
16
17 //tss code here
18
19 //call superclass method
20 super.onActivityResult(requestCode, resultCode, data);
21 }
Here we retrieve the result of the speech recognition process. Notice that the "if"
statement checks to see if the request code is the variable we passed when calling
"startActivityForResult", in which case we know this method is being called as a
result of the listening Intent. The recognizer returns the list of 10 suggested words,
which we store as an Array List. We then populate the List View with these words,
by setting an Array Adapter object as Adapter for the View. Now each of the items
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
by setting an Array Adapter object as Adapter for the View. Now each of the items
in the List View will display one of the suggested words.
If the app successfully recognizes the user input speech and returns the list of
words, it will appear as follows:
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Alternatively, if the app does not recognize the user speech input, the following
screen will appear:
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Step 6: Detect User Word Choices
We want to detect the user selecting words from the list, so let's implement a click
listener for the list items. Back in your "onCreate" method, after the existing code,
set the listener for each item in the list as follows:
The user can press the touchscreen or use a trackball to select words in the list.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
When the user selects a word, the Toast message appears confirming it.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Step 7: Setup TTS Functionality
If you do not want to implement the Text To Speech functionality, you can stop now
and test your app. We only require a little more processing to make our app repeat
the user's chosen word. First, to set up the TTS engine, add the following code to
the section in your "onCreate" method where you queried the system for speech
recognition support. Inside the "if" statement, after
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
"speechBtn.setOnClickListener(this);":
Like the speech listening process, we will receive the result of this code checking
for TTS data in the "onActivityResult" method. In that method, before the line in
which we call the superclass "onActivityResult" method, add the following:
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Here we initialize the TTS if the data is already installed, otherwise we prompt the
user to install it. For additional guidance on using the TTS engine, see the Android
SDK: Using the Text to Speech Engine tutorial.
To complete TTS setup, add the "onInit" method to your class declaration, handling
initialization of the TTS as follows:
1 /**
2 * onInit fires when TTS initializes
3 */
4 public void onInit(int initStatus) {
5 //if successful, set locale
6 if (initStatus == TextToSpeech.SUCCESS)
7 repeatTTS.setLanguage(Locale.UK);
//***choose your own locale here***
8 }
Here we simply set the Locale for the TTS, but you can carry out other setup tasks
if you like.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Advertisement
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
This will cause the app to repeat the user's chosen word as part of a simple phrase.
This will occur at the same time the Toast message appears.
Conclusion
That's our complete Speak and Repeat app. Test it on an Android device with
speech recognition and TTS support - the emulator does not support speech
recognition so you need to test this functionality on an actual device. The
source code is attached, so you can check if you have everything in the right place.
Of course, your own apps may implement speech recognition as part of other
processing, but this tutorial should have equipped you with the essentials of
supporting speech input.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Advertisement
Translations Available:
Jobs
WordPress Developer
at WebDevStudios in Los Angeles, CA,
USA
34 Comments Mobiletuts+
Sort by Best
Thanks a ton!!
6 Reply Share
But What about, Listen -> Repeat -> Listen again? What I am trying to achieve is after app listens user, It
speak back asking for Yes/No Confirmation and based on that further processing should happen. When I
again (in this case listenToSpeech(), It runs the code without waiting for Speaker to let finish speaking. ca
please point me in right direction?
2 Reply Share
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
BitX 2 years ago
Thanks Sue!.
Is very Useful
Reply Share
thanks before
Reply Share
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Maximiliano 2 years ago
Hello friends,
When I reach the past part of Step 2, this is: Create a new file in res/layout named word.xmland then e
following code:
I get the following error: [2012-10-22 00:47:02 - testspeech] Error in an XML file: aborting build.
Thank you.
Reply Share
i have try the source code, but when i have speech, before the list of word show, the app suddenly force c
However, I have encountered another problem. An installed .apk cannot be launched in an actual d
Reply Share
You shouldn't attempt to run the app until the steps are completed, I've only included a screen shot so tha
see what the code is building. The error is because you haven't added the onInit method yet, which you do
Sue
Reply Share
import android.app.Activity;
import android.content.DialogInterface;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.view.View.OnClickListener;
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.TextView;
see more
Reply Share
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Advertisement
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Follow Us Email Newsletters
FAQ
Subscribe
Terms of Use
Contact Support Privacy Policy
About Tuts+
Advertise
Teach at Tuts+
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
2014 Envato Pty Ltd. Trademarks and brands are the property of their respective
owners.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com