一、概述
TextToSpeech,就是将文本内容转换成语音,在其他的一些应用中经常可以看到。这个功能还是挺强大的,但是用户利用它来编写应用却很简单。
二、要求
能够将文本内容转换成语音并朗读出来;可以一次全部朗读出来,也可以边写边读;可以将文本保存为语音文件。
三、实现
新建工程MySpeak,修改/res/layout/main.xml文件,在里面添加一个EditText,两个Button和一个CheckBox,完整的main.xml文件如下:
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3android:layout_width="fill_parent"
4android:layout_height="fill_parent"
5android:orientation="vertical" >
6
7<EditText
8android:id="@+id/edittext"
9android:layout_width="fill_parent"
10android:layout_height="wrap_content"
11/>
12
13<Button
14android:id="@+id/rbutton"
15android:layout_width="fill_parent"
16android:layout_height="wrap_content"
17android:text="朗读"
18/>
19
20<Button
21android:id="@+id/sbutton"
22android:layout_width="fill_parent"
23android:layout_height="wrap_content"
24android:text="保存"
25/>
26
27<CheckBox
28android:id="@+id/checkbox"
29android:layout_width="fill_parent"
30android:layout_height="wrap_content"
31android:text="边写边读"
32android:checked="true"
33/>
34
35
36 </LinearLayout>
修改MySpeakActivity.java文件,设置两个Button按钮的监听和EditText的内容变化监听,完整的MySpeakActivity.java内容如下:
1 package com.nan.speak;
2
3 import java.util.Locale;
4
5 import android.app.Activity;
6 import android.os.Bundle;
7 import android.speech.tts.TextToSpeech;
8 import android.text.Editable;
9 import android.text.TextWatcher;
10 import android.view.View;
11 import android.widget.Button;
12 import android.widget.CheckBox;
13 import android.widget.EditText;
14 import android.widget.Toast;
15
16
17 public class MySpeakActivity extends Activity
18 {
19private EditText mEditText = null;
20private Button readButton = null;
21private Button saveButton = null;
22private CheckBox mCheckBox = null;
23private TextToSpeech mTextToSpeech = null;
24
25/** Called when the activity is first created. */
26@Override
27public void onCreate(Bundle savedInstanceState)
28{
29super.onCreate(savedInstanceState);
30setContentView(R.layout.main);
31
32mEditText = (EditText)this.findViewById(R.id.edittext);
33readButton = (Button)this.findViewById(R.id.rbutton);
34saveButton = (Button)this.findViewById(R.id.sbutton);
35mCheckBox = (CheckBox)this.findViewById(R.id.checkbox);
36//实例并初始化TTS对象
37mTextToSpeech = new TextToSpeech(this,new TextToSpeech.OnInitListener()
38{
39
40@Override
41public void onInit(int status)
42{
43// TODO Auto-generated method stub
44if(status == TextToSpeech.SUCCESS)
45{
46//设置朗读语言
47int supported = mTextToSpeech.setLanguage(Locale.US);
48if((supported != TextToSpeech.LANG_AVAILABLE)&&(supported != TextToSpeech.LANG_COUNTRY_AVAILABLE))
49{
50displayToast("不支持当前语言!");
51}
52}
53}
54
55});
56//朗读按钮监听
57readButton.setOnClickListener(new View.OnClickListener()
58{
59
60@Override
61public void onClick(View v)
62{
63// TODO Auto-generated method stub
64//朗读EditText里的内容
65mTextToSpeech.speak(mEditText.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
66}
67});
68//保存按钮监听
69saveButton.setOnClickListener(new View.OnClickListener()
70{
71
72@Override
73public void onClick(View v)
74{
75// TODO Auto-generated method stub
76
77//将EditText里的内容保存为语音文件
78int r = mTextToSpeech.synthesizeToFile(mEditText.getText().toString(), null, "/mnt/sdcard/speak.wav");
79if(r == TextToSpeech.SUCCESS)
80displayToast("保存成功!");
81}
82});
83//EditText内容变化监听
84mEditText.addTextChangedListener(mTextWatcher);
85
86}
87
88
89private TextWatcher mTextWatcher = new TextWatcher()
90{
91
92@Override
93public void afterTextChanged(Editable s)
94{
95// TODO Auto-generated method stub
96//如果是边写边读
97if(mCheckBox.isChecked()&&(s.length()!=0))
98{
99//获得EditText的所有内容
100String t = s.toString();
101mTextToSpeech.speak(t.substring(s.length()-1), TextToSpeech.QUEUE_FLUSH, null);
102}
103}
104
105@Override
106public void beforeTextChanged(CharSequence s, int start, int count,
107int after)
108{
109// TODO Auto-generated method stub
110
111}
112
113@Override
114public void onTextChanged(CharSequence s, int start, int before,
115int count)
116{
117// TODO Auto-generated method stub
118
119}
120};
121
122//显示Toast函数
123private void displayToast(String s)
124{
125Toast.makeText(MySpeakActivity.this, s, Toast.LENGTH_SHORT).show();
126}
127
128
129@Override
130public void onDestroy()
131{
132super.onDestroy();
133
134if(mTextToSpeech != null)
135mTextToSpeech.shutdown();//关闭TTS
136}
137
138 }
最后,在AndroidManifest.xml文件中加入权限:
1 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
好了,运行该程序