Android app Development improved (2)-----text aloud TTS (texttospeech)

Source: Internet
Author: User
Tags gettext
<span id="Label3"></p><p><p>Link Address: http://www.cnblogs.com/lknlfy/archive/2012/02/26/2368696.html</p></p><p><p><strong>I. Overview</strong></p></p><p><p>Texttospeech is the conversion of text content to speech, which is often seen in other applications. This feature is pretty powerful, but it's easy for users to use it to write apps.</p></p><p><p></p></p><p><p><strong>second, The requirements</strong></p></p><p><p>The ability to convert text content to speech and read aloud, or to write while reading; you can save text as a speech file.</p></p><p><p></p></p><p><p><strong>third, the realization</strong></p></p><p><p>New Project myspeak, Modify The/res/layout/main.xml file, Add a edittext, two buttons and a checkbox in it, complete the Main.xml file as Follows:</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre>1 <?xml version= "1.0" encoding= "utf-8"?><br>2 <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"<br>3 android:layout_width= "fill_parent"<br>4 android:layout_height= "fill_parent"<br>5 android:orientation= "vertical" ><br>6<br>7 <edittext<br>8 android:id= "@+id/edittext"<br>9 Android:layout_width= "fill_parent"<br>Ten android:layout_height= "wrap_content"<br>/><br>12<br><button<br>Android:id= "@+id/rbutton"<br>Android:layout_width= "fill_parent"<br>android:layout_height= "wrap_content"<br>android:text= "read aloud"<br>/><br>19<br><button<br>Android:id= "@+id/sbutton"<br>Android:layout_width= "fill_parent"<br>android:layout_height= "wrap_content"<br>android:text= "save"<br>/><br>26<br><checkbox<br>Android:id= "@+id/checkbox"<br>Android:layout_width= "fill_parent"<br>android:layout_height= "wrap_content"<br>android:text= "write and read"<br>Android:checked= "true"<br>/><br>34<br>35<br></LinearLayout></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>Modify the Myspeakactivity.java file, set two button buttons for listening and edittext content change monitoring, complete Myspeakactivity.java content as Follows:</p></p><p><p></p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre>1 package com.nan.speak;<br>2<br>3 Import java.util.Locale;<br>4<br>5 Import android.app.Activity;<br>6 Import android.os.Bundle;<br>7 Import android.speech.tts.TextToSpeech;<br>8 Import android.text.Editable;<br>9 Import android.text.TextWatcher;<br>Ten Import android.view.View;<br>Import android.widget.Button;<br>Import android.widget.CheckBox;<br>Import android.widget.EditText;<br>Import android.widget.Toast;<br>15<br>16<br>public class Myspeakactivity extends Activity<br>18 {<br>Private EditText Medittext = null;<br>Private Button Readbutton = null;<br>Private Button Savebutton = null;<br>The private CheckBox Mcheckbox = null;<br>Texttospeech Mtexttospeech = null;<br>24<br>/** called when the activity is first Created. */<br>@Override<br>public void OnCreate (Bundle Savedinstancestate)<br>28 {<br>Super.oncreate (savedinstancestate);<br>Setcontentview (r.layout.main);<br>31<br>Medittext = (EditText) This.findviewbyid (r.id.edittext);<br>Readbutton = (Button) This.findviewbyid (r.id.rbutton);<br>Savebutton = (Button) This.findviewbyid (r.id.sbutton);<br>Mcheckbox = (CheckBox) This.findviewbyid (r.id.checkbox);<br>36//instance and Initialize TTS object<br>PNS Mtexttospeech = new Texttospeech (this,new texttospeech.oninitlistener ()<br>38 {<br>39<br>@Override<br>$ public void OnInit (int Status)<br>42 {<br>+//TODO auto-generated Method Stub<br>if (status = = Texttospeech.success)<br>45 {<br>46//set Spoken language<br>Supported int = Mtexttospeech.setlanguage (locale.us);<br>(supported! = Texttospeech.lang_available) && (supported! = Texttospeech.lang_country_avai Lable))<br>49 {<br>Displaytoast ("does not support the current language! ");<br>51}<br>52}<br>53}<br>54<br>55});<br>56//read Button Monitor<br>Readbutton.setonclicklistener (new View.onclicklistener ()<br>58 {<br>59<br>@Override<br>public void OnClick (View V)<br>62 {<br>+//TODO auto-generated Method Stub<br>64//read aloud the contents of EditText<br>Mtexttospeech.speak (medittext.gettext (). toString (), texttospeech.queue_flush, null);<br>66}<br>67});<br>68//save Button Monitor<br>Savebutton.setonclicklistener (new View.onclicklistener ()<br>70 {<br>71<br>@Override<br>public void OnClick (View V)<br>74 {<br>//TODO auto-generated Method Stub<br>76<br>77//save content in EditText as a voice file<br>int r = mtexttospeech.synthesizetofile (medittext.gettext (). toString (), null, "/mnt/sdcard/speak.wav");<br>if (r = = Texttospeech.success)<br>Displaytoast ("save success! ");<br>81}<br>82});<br>//edittext Content Change Monitoring<br>Medittext.addtextchangedlistener (mtextwatcher);<br>85<br>86}<br>87<br>88<br>Private Textwatcher Mtextwatcher = new Textwatcher ()<br>90 {<br>91<br>@Override<br>aftertextchanged public void (Editable S)<br>94 {<br>Up//TODO auto-generated Method Stub<br>96//if you are writing while reading<br>If (mcheckbox.ischecked () && (s.length ()!=0))<br>98 {<br>99//get all the content of EditText<br>The String t = s.tostring ();<br>101 Mtexttospeech.speak (t.substring (s.length ()-1), texttospeech.queue_flush, null);<br>102}<br>103}<br>104<br>@Override<br>106 public void beforetextchanged (charsequence s, int start, int count,<br>107 int After)<br>108 {<br>109//TODO auto-generated Method Stub<br>110<br>111}<br>112<br>113 @Override<br>ontextchanged public void (charsequence s, int start, int before,<br>(+ + int Count)<br>116 {<br>117//TODO auto-generated Method Stub<br>118<br>119}<br>120};<br>121<br>122//show Toast function<br>123 private void Displaytoast (String S)<br>124 {<br>Toast.maketext (myspeakactivity.this, s, toast.length_short). show ();<br>126}<br>127<br>128<br>129 @Override<br>OnDestroy public void ()<br>131 {<br>Super.ondestroy ();<br>133<br>134 If (mtexttospeech! = Null)<br>135 Mtexttospeech.shutdown ();//turn off TTS<br>136}<br>137<br>138}</pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>finally, Add the permissions in the Androidmanifest.xml file:</p></p><p><p></p></p><pre><pre>1 <uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE"/></pre></pre><p><p>ok, Run the Program:</p></p><p><p></p></p><p><p></p></p><p><p>Input "123456789", You can hear the input of a word immediately be read out,</p></p><p><p></p></p><p><p>Description</p></p><p><p>I do not know why, in a real machine on my test can not read out, suggesting that language is not supported, on another platform Can.</p></p><p><p>Android app Development improved (2)-----text aloud TTS (texttospeech)</p></p></span>

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.