Android TTS text to speech development

Source: Internet
Author: User

<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " > Before the development of TTS to be able to normal text to speech, but today did a small program, the results are not sound, carefully tested a bit, found a problem. </span>

First of all, how to achieve the TTS.

1, install the Voice library, if you want to Chinese pronunciation, Iflytek Voice 3.0 is very good.

2, the simplest program is as follows:

Package Com.example.tts;import Java.util.locale;import Android.speech.tts.texttospeech;import Android.support.v7.app.actionbaractivity;import Android.os.bundle;import Android.view.menu;import Android.view.menuitem;import Android.widget.toast;public class Mainactivity extends Actionbaractivity implements Texttospeech.oninitlistener{texttospeech Texttospeech = null; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); TextToSpeech = new Texttospeech (this, this); Texttospeech.speak ("Silent Here", texttospeech.queue_add, null);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true;}    protected void OnDestroy () {Super.ondestroy ();       if (texttospeech!=null) {Texttospeech.shutdown ();} } @Overridepublic Boolean onoptionsitemselected (MenuItem item) {int id = item.getitemid (); if (id = = r.id.action_settings) {Texttospeech.speak ("sound Here", Texttospeech. Queue_flush, null); return true;} return super.onoptionsitemselected (item);}              @Overridepublic void onInit (int status) {//TODO auto-generated method stubif (Status = = Texttospeech.success) {                      int result = Texttospeech.setlanguage (Locale.chinese);                    if (result = = Texttospeech.lang_missing_data | | result = = texttospeech.lang_not_supported ||              result = = Texttospeech.error) {Toast.maketext (this, "data loss or language not supported", Toast.length_short). Show (); } if (result = = texttospeech.lang_available) {Toast.maketext (this, "support for the language", Toast.length_shor  T). Show ();          } toast.maketext (This, "initialization succeeded", Toast.length_short). Show (); } }}

Without any permissions, there is a problem here, that is, after creating an object dynamically, the Speak method is called in the OnCreate, and no sound is emitted.

You can save text as a voice file, or you can read a voice file

public void SaveToFile (Texttospeech speech,string text,string file) {String destfilename = "/sdcard/tts/" +file+ ". wav"; Speech.synthesizetofile (text, NULL, destfilename);} public void ReadFromFile (Texttospeech speech,string file) {String destfilename = "/sdcard/tts/" +file+ ". wav"; Speech.addspeech ("2", destFileName); Speech.Speak ("2", texttospeech.queue_add, null);}

That's all you can do.

Next talk about how to implement speech recognition



Android TTS text to speech development

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.