Android learning notes (18) ---- use TTS to develop an android pronunciation dictionary

Source: Internet
Author: User

/*************************************** **************************************** *************
* Author: conowen @ Dazhong
* E-mail: conowen@hotmail.com
* Http://blog.csdn.net/conowen
* Note: This article is original and only used for learning and communication. For more information, indicate the author and its source.

**************************************** **************************************** ************/


1. Introduction to tts:

Texttospeech (TTS) is supported in Android 1.6. The TTS engine can convert the specified text to audio output in different languages. The text-to-speech Conversion Box output process is completed by the pico speech synthesis engine of the Android system. Currently, TTS in Android supports five languages: English, French, German, Italian, and Spanish. Currently, TTS does not support Chinese. For more information, see open-source projects on the Internet, there are many TTS engines that support Chinese characters.

Of course, you can also install the TTS engine by yourself. After installation, you can select the default engine in "Settings"> "Voice Input and Output" ------> "text-to-speech Settings.

If "always use my settings" is selected, the system will set the highest priority here, And the settings in the Code are invalid.

 

2. parsing the XML dictionary using the pull Method

For this part, refer to the previous blog post. Http://blog.csdn.net/conowen/article/details/7420298

This article adds the TTS pronunciation function based on the above blog posts.

3. Note


Some Android systems may reduce the TTS engine. You can view the TTS engine in the figure.

After TTS is used, disable TTS through the ondestroy method of the activity; otherwise, the following error will be thrown.

04-02 11:25:03.700: E/ActivityThread(4397): Activity com.conowen.Dictionary.DictionaryActivity has leaked ServiceConnection android.speech.tts.TextToSpeech$1@406540f0 that was originally bound here04-02 11:25:03.700: E/ActivityThread(4397): android.app.ServiceConnectionLeaked: Activity com.conowen.Dictionary.DictionaryActivity has leaked ServiceConnection android.speech.tts.TextToSpeech$1@406540f0 that was originally bound here

4. Simple demo

This demo parses the XML dictionary using the pull method, and then uses the TTS pronunciation to create a simple Android pronunciation dictionary.

/* Author: conowen * Date: 2012.4.2 */package COM. conowen. dictionary; import Java. util. locale; import android. app. activity; import android. content. res. xmlresourceparser; import android. OS. bundle; import android. speech. TTS. texttospeech; import android. speech. TTS. texttospeech. oninitlistener; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. EDI Ttext; import android. widget. textview; public class dictionaryactivity extends activity implements oninitlistener {// introduces the oninitlistener interface to initialize ttsstring input = NULL; string match = NULL; edittext ET = NULL; button search = NULL; button del = NULL; textview display = NULL; texttospeech speak = NULL; // define ttsbutton speakbt = NULL; // TTS pronunciation button string speakstr = ""; // pronunciation text/** called when the activity is first creat Ed. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); ET = (edittext) findviewbyid (R. id. et); Search = (button) findviewbyid (R. id. search); del = (button) findviewbyid (R. id. del); display = (textview) findviewbyid (R. id. TV); speakbt = (button) findviewbyid (R. id. speakbt); speak = new texttospeech (this, this); // new only, the system will call the TTS initialization function oni Nit (). All the oninit () methods to be rewritten to search. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {// todo auto-generated method stubinput = et. gettext (). tostring (); // create an XML folder under the res directory of the current project, and drag cet4.xml into xmlresourceparser xrp = getresources (). getxml (R. XML. CET4); // define an XML resource parser try {stringbuilder strbuilder = new stringbuilder (""); While (xrp. geteventtype ()! = Xmlresourceparser. end_document) {// If (xrp. geteventtype () = xmlresourceparser. start_tag) {// when a start tag is encountered, it includes all the tags in the sub-tag and the sub-tag string tagname = xrp. getname (); // obtain the tag name if (tagname. equals ("word") {// retrieves the word match = xrp. nexttext (); // read the content in the tag, that is, the word if (match. equals (input) {// If the obtained word is the same as the input word xrp. next (); // read the next tag, that is, the <trans> translation strbuilder. append (xrp. nexttext (); // extract the translation display. settext (strbuild Er); // display translation speakstr = match; // assign a value to the pronunciation text break; }}} xrp. next (); // read the next tag }}catch (exception e) {// todo: handle exception }}); speakbt. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {// todo auto-generated method stubspeak. speak (speakstr, texttospeech. queue_add, null) ;}}); Del. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {// todo auto-Genera Ted method stubdisplay. settext (""); et. settext (""); speakstr = "" ;}}) ;}@ overridepublic void oninit (INT status) {// todo auto-generated method stubspeak. setlanguage (locale. US); // initialize the TTS component and set the language to US English} @ overrideprotected void ondestroy () {// todo auto-generated method stubsuper. ondestroy (); If (speak! = NULL) Speak. Shutdown (); // disable the TTS engine }}

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.