Android TTS learning-TTS initial experience

Source: Internet
Author: User
Tags time zones

I.
Basic knowledge

Texttospeech

Abbreviation
TTS

,
Called speech synthesis,
Yes
Android
Slave
Version 1.6

Start to support
New Features
, Can
Converts the specified text to audio output in different languages.

Before proceeding to the details, let's take a look at the TTS setting interface of the adnroid system and click Settings-> speech synthesis, as shown in:

Figure 1 Android TTS settings page

When you click "listen to an example", the"
This is an example of speech synthesis in English.
", If not installed
For TTS data, click the second installation item
Default settings
Configurable
Speech Rate
And
Language
, Such
2
As shown in
TTS
Supported languages.


Figure 2 TTS
Language Selection

If you select "always use my settings ",
Note that after this option is set, we cannot
Speech Rate
And
Ages
Set. The settings here overwrite the settings in our program.

The TTS function requires the support of TTS engine. Let's take a look at the TTS engine provided by Android.

Android uses a speech synthesis engine called Pico that supports multiple languages. Pico is responsible for analyzing input text in the background and dividing the text into different parts that he can recognize, then we can connect the synthesized speech fragments in a natural way. In this process, the android system will help us with this process. We only need to regard it as a magical process.

TTS engine depends on the current
Android platform
Supported languages:
English
,
French
,
German
,
Italian
And
Spanish
Five languages

(Chinese characters are not supported at the moment ).
TTS can convert texts to Speech Output in any of the above five languages. At the same time, for individual language versions, it depends on different time zones. For example:
English
, In
TTS
Can output two different versions: American and English.

.

II.
Instance analysis

Next we will first make a specific example to learn about the effect of TTS on Android.

Let's make a simple example of inputting an English sentence and then read it out to understand how to use TTS in Android.

Before specific steps, give a link to the androidtts API,

Http://androidappdocs.appspot.com/reference/android/speech/tts/package-summary.html

Let's take a look at the functions provided by TTS.

Figure 3 Android TTS API

As you can see, TTS provides two interfaces and two classes.

We hope to achieve the following results:

Figure 4 Implementation

In the input box above, click the Speak button to read the text above.

Create an android project named androidttsdemofirst. the SDK must be version 1.6 or later.

The main. xml file is simple, as shown below:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <edittext Android: id = "@ + ID/inputtext" <br/> Android: hint = "input the text here! "<Br/> Android: layout_width =" fill_parent "<br/> Android: layout_height =" wrap_content "> <br/> </edittext> <br/> <button Android: TEXT = "speak" <br/> Android: Id = "@ + ID/speakbtn" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: layout_gravity = "center_horizontal" <br/> Android: enabled = "false" <br/> </button> <br/> </linearlayout>

Java file Compilation:

Use TTS to implement the oninitlistener Interface

Public class androidttsdemofirst extends activity implements oninitlistener {<br/>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/>}< br/> // implement the TTS initialization interface <br/> @ override <br/> Public void oninit (INT status) {<br/> // todo auto-generated method stub </P> <p >}< br/>}

Then define the following variables:

Private edittext inputtext = NULL; <br/> private button speakbtn = NULL; <br/> Private Static final int req_tts_status_check = 0; <br/> Private Static final string tag = "TTS demo"; <br/> private texttospeech MTTS;

Although the TTS engine is installed on Android 1.6 and later Android machines by default, some devices may lack the resource files corresponding to the installation and language due to storage capacity restrictions, therefore, before using TTS, you must check whether TTS language data is installed and add the following content to the oncreate function:

// Check whether TTS data is installed and available <br/> intent checkintent = new intent (); <br/> checkintent. setaction (texttospeech. engine. action_check_tts_data); <br/> startactivityforresult (checkintent, req_tts_status_check );

Start a new intent to check whether the TTS data has been installed. We will process it based on the returned results, as shown below:

Protected void onactivityresult (INT requestcode, int resultcode, intent data) {<br/> If (requestcode = req_tts_status_check) <br/>{< br/> switch (resultcode) {<br/> case texttospeech. engine. check_voice_data_pass: <br/> // The returned result indicates that TTS engine can use <br/>{< br/> MTTS = new texttospeech (this, this ); <br/> log. V (TAG, "TTS engine is installed! "); </P> <p >}</P> <p> break; <br/> case texttospeech. engine. check_voice_data_bad_data: <br/> // The required voice data is damaged. <br/> case texttospeech. engine. check_voice_data_missing_data: <br/> // The voice data in the required language is missing <br/> case texttospeech. engine. check_voice_data_missing_volume: <br/> // missing pronunciation data for the required language <br/>{< br/> // all three cases indicate data errors, re-download and install the required data <br/> log. V (TAG, "need language stuff:" + resultcode); <br/> intent dataintent = new intent (); <br/> dataintent. setaction (texttospeech. engine. action_install_tts_data); <br/> startactivity (dataintent); </P> <p >}< br/> break; <br/> case texttospeech. engine. check_voice_data_fail: <br/> // check failed <br/> default: <br/> log. V (TAG, "Got a failure. TTS apparently not available "); <br/> break; <br/>}< br/> else <br/>{< br/> // other intent returned results <br/>}< br/>}

If check_voice_data_pass is returned, the check is successful. You can create a new
Texttospeech
Otherwise, download
TTS data.

After a texttospeech is successfully created for TTS data detection, the callback function defined in oninitlistener is called.

// Implement the TTS initialization interface <br/> @ override <br/> Public void oninit (INT status) {<br/> // todo auto-generated method stub <br/> // TTS engine Initialization is complete <br/> If (status = texttospeech. success) <br/>{< br/> int result = MTTS. setlanguage (locale. US); <br/> // set the pronunciation language <br/> If (result = texttospeech. lang_missing_data | result = texttospeech. lang_not_supported) <br/> // determine whether the language is available <br/>{< br/> log. V (TAG, "language is not available"); <br/> speakbtn. setenabled (false); <br/>}< br/> else <br/>{< br/> MTTS. speak ("this is an example of speech synthesis. ", texttospeech. queue_add, null); <br/> speakbtn. setenabled (true); <br/>}</P> <p>}

In this callback function, we can set the language and then use it.
The TTS engine is operating.

Creating a texttospeech requires resource occupation. Therefore, we need to release this resource in due time:

@ Override <br/> protected void onpause () {<br/> // todo auto-generated method stub <br/> super. onpause (); <br/> If (MTTS! = NULL) <br/> // TTS is also stopped when the activity is paused <br/>{< br/> MTTS. stop (); <br/>}</P> <p> @ override <br/> protected void ondestroy () {<br/> // todo auto-generated method stub <br/> super. ondestroy (); <br/> // release TTS resources <br/> MTTS. shutdown (); <br/>}

Finally, Set
Use of edittext and button:

Inputtext = (edittext) findviewbyid (R. id. inputtext); <br/> speakbtn = (button) findviewbyid (R. id. speakbtn); <br/> inputtext. settext ("this is an example of speech synthesis. "); <br/> speakbtn. setonclicklistener (New onclicklistener () {</P> <p> Public void onclick (view v) {<br/> // todo auto-generated method stub <br/> MTTS. speak (inputtext. gettext (). tostring (), texttospeech. queue_add, null); <br/> // read the content in the input box <br/>}< br/> });

So far, our
The demo is complete. After the program is started, the"
This is an example of speech synthesis in English.
Then you enter the content in edittext and click the Speak button to read the content.

The complete code example corresponding to the article can be downloaded here:

Http://download.csdn.net/source/2610740

Note:"
The first Google summer College Students' blog sharing competition-2010 andriod"

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.