Mobile voice engine development notes

Source: Internet
Author: User

With the increasing popularity of mobile applications, mobile voice applications are becoming more and more popular among users. It is easy to use the voice engine in Android. It can be implemented through several steps. This article will guide you how to use the voice engine in Android SDK to create simple applications.
In this tutorial, we will teach you how to create a speech application step by step. The specific code is downloaded at the end of the article.
Step 1 create an Android Project
First, we create an Android project, which uses Android 2. 3.
Step 2 create a user interface
Because our example is very simple, you only need to add some controls to the interface. We can open main. In the xml file, write the following code and add a text box and a button:

View source print?
01 <TextView android: id = "@ + id/intro"
02 Android: layout_width = "fill_parent"
03 Android: layout_height = "wrap_content"
04 Android: text = "Enter some text :"
05 />
06 <EditText android: id = "@ + id/enter"
07 Android: layout_width = "fill_parent"
08 Android: layout_height = "wrap_content"
09 />
10 <Button android: id = "@ + id/speak"
11 Android: layout_width = "wrap_content"
12 Android: layout_height = "wrap_content"
13 Android: text = "Speak"
14 />




Step 3 Write listener events
Next, we add event processing for the button. First, we need to obtain the text entered by the user. The Code is as follows:
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. view. View;
Import android. widget. EditText;
Public class SpeakingAndroid extends Activity implements OnClickListener
{
// Create the Activity
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Button speakButton = (Button) findViewById (R. id. speak );
// Listen for button events
SpeakButton. setOnClickListener (this );
}
// Response code of The onClick event
Public void onClick (View v ){
// Obtain the text entered by the user
EditText enteredText = (EditText) findViewById (R. id. enter );
String words = enteredText. getText (). toString ();
// Call the voice Engine
SpeakWords (words );
}
}
Step 4 compile the relevant voice Methods
First, import the relevant voice engine package. The Code is as follows:
Import android. speech. tts. TextToSpeech;
Import android. speech. tts. TextToSpeech. OnInitListener;
Next, in order to be able to check the preparation for running the voice engine, the system must first check the data before it runs. Therefore, first declare a member variable, as shown below:
Private int MY_DATA_CHECK_CODE = 0;
In the oncreate method, write the following code:
Intent checkTTSIntent = new Intent ();
CheckTTSIntent. setAction (TextToSpeech. Engine. ACTION_CHECK_TTS_DATA );
StartActivityForResult (checkTTSIntent, MY_DATA_CHECK_CODE );
The purpose of the above Code is to create an Intent to check whether the data required by TTS is ready. If so, call the onActivityResult method, which will be mentioned below.
Step 5 create a TTS instance
In the onActivityResult method, you can start to create an instance of the TTS voice engine. The Code is as follows:
[Java]
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
If (requestCode = MY_DATA_CHECK_CODE ){
If (resultCode = TextToSpeech. Engine. CHECK_VOICE_DATA_PASS ){
MyTTS = new TextToSpeech (this, this );
}
Else {
Intent installTTSIntent = new Intent ();
InstallTTSIntent. setAction (TextToSpeech. Engine. ACTION_INSTALL_TTS_DATA );
StartActivity (installTTSIntent );
}
}
}
After the data check of TTS is complete, this method is called. The resultCode indicates whether TTS verification is successful. If the verification is successful, myTTS = new TextToSpeech (this, this );
Create a voice TTS instance. If you fail to pass the TTS ready installation test, a new Intent will be prompted to guide the user to the interface for installing the TTS engine.
Step 6 initialize engine data
Since the OnInitListener interface is implemented in this program, you can initialize and set the Speech engine in the oninit method, such as setting language. First, you should import the relevant class libraries as follows:
Import java. util. Locale;
Write the following code in the oninit method:
Public void onInit (int initStatus ){
If (initStatus = TextToSpeech. SUCCESS ){
MyTTS. setLanguage (Locale. US );
}
Else if (initStatus = TextToSpeech. ERROR ){
Toast. makeText (this, "Sorry! Text To Speech failed... ", Toast. LENGTH_LONG). show ();
}
}
If TTS is ready, set the setLanguage method to use the corresponding language (
If it is determined that TTS cannot be ready, the Toast display method is called to notify the user.
Step 7 Let TTS speak
After preparing the above steps, the last step is to let TTS speak. In the speakWords method, write the following code:
Private void speakWords (String speech ){
MyTTS. speak (speech, TextToSpeech. QUEUE_FLUSH, null );
}
Many parameters are available in the speak method. For more information, see the Android documentation.
The TextToSpeech. QUEUE_FLUSH parameter can be used to enable the TTS voice engine to continuously read data.
MyTTS. speak (speech, TextToSpeech. QUEUE_ADD, null) parameter. If the application is not required to read aloud, you can add the following sentence:
MyTTS. shutdown ();
However, unless it is clear that the user does not need to read aloud from the application, this code should not be added if the system needs to read aloud continuously.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.