<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