创建一个Android工程,工程名为AndroidTTSDemoFirst,其中SDK必须选择1.6版本及以上。
其中Main.xml文件很简单,如下所示:
- xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android=""
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <EditText android:id="@+id/inputText"
- android:hint="Input the text here!"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- EditText>
- <Button android:text="Speak"
- android:id="@+id/speakBtn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:enabled="false"
- >Button>
- LinearLayout>
Java文件的编写:
要使用TTS得实现OnInitListener接口
- public class AndroidTTSDemoFirst extends Activity implements OnInitListener {
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
-
- @Override
- public void onInit(int status) {
-
-
- }
- }
接着定义好下面要用到的几个变量:
- private EditText inputText = null;
- private Button speakBtn = null;
- private static final int REQ_TTS_STATUS_CHECK = 0;
- private static final String TAG = "TTS Demo";
- private TextToSpeech mTts;
虽然安装Android1.6版本及之后的Android机器都默认安装了TTS引擎,但某些设备可能会因为存储容量的限制而缺少安装和语言对应的资源文件,因此在使用TTS前,得先要检查是否安装了TTS语言数据,在onCreate函数中添加以下内容:
-
- Intent checkIntent = new Intent();
- checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
- startActivityForResult(checkIntent, REQ_TTS_STATUS_CHECK);
这里启动一个新的Intent去检查TTS数据是否已经安装,我们根据其返回的结果进行处理,如下所示:
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if(requestCode == REQ_TTS_STATUS_CHECK)
- {
- switch (resultCode) {
- case TextToSpeech.Engine.CHECK_VOICE_DATA_PASS:
-
- {
- mTts = new TextToSpeech(this, this);
- Log.v(TAG, "TTS Engine is installed!");
-
- }
-
- break;
- case TextToSpeech.Engine.CHECK_VOICE_DATA_BAD_DATA:
-
- case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_DATA:
-
- case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_VOLUME:
-
- {
-
- Log.v(TAG, "Need language stuff:"+resultCode);
- Intent dataIntent = new Intent();
- dataIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
- startActivity(dataIntent);
-
- }
- break;
- case TextToSpeech.Engine.CHECK_VOICE_DATA_FAIL:
-
- default:
- Log.v(TAG, "Got a failure. TTS apparently not available");
- break;
- }
- }
- else
- {
-
- }
- }
如果返回CHECK_VOICE_DATA_PASS表示检查成功,可以新建一个 TextToSpeech ,否则就去下载 TTS数据。
当TTS数据检测成功创建一个TextToSpeech后,就会调用接口OnInitListener中定义的回调函数
-
- @Override
- public void onInit(int status) {
-
-
- if(status == TextToSpeech.SUCCESS)
- {
- int result = mTts.setLanguage(Locale.US);
-
- if(result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED)
-
- {
- Log.v(TAG, "Language is not available");
- speakBtn.setEnabled(false);
- }
- else
- {
- mTts.speak("This is an example of speech synthesis.", TextToSpeech.QUEUE_ADD, null);
- speakBtn.setEnabled(true);
- }
- }
-
- }
在这个回调函数里,我们设置语言,然后就可以进行使用 TTS引擎进行操作了。
创建一个TextToSpeech是需要占用资源的,因此我们要适时的释放这个资源:
- @Override
- protected void onPause() {
-
- super.onPause();
- if(mTts != null)
-
- {
- mTts.stop();
- }
- }
-
- @Override
- protected void onDestroy() {
-
- super.onDestroy();
-
- mTts.shutdown();
- }
最后,在onCreate函数中设置 EditText和Button的使用:
- inputText = (EditText)findViewById(R.id.inputText);
- speakBtn = (Button)findViewById(R.id.speakBtn);
- inputText.setText("This is an example of speech synthesis.");
- speakBtn.setOnClickListener(new OnClickListener() {
-
- public void onClick(View v) {
-
- mTts.speak(inputText.getText().toString(), TextToSpeech.QUEUE_ADD, null);
-
- }
- });
至此,我们的 Demo就完成了,当启动程序后,首先会朗读“ This is an example of speech synthesis in English. ”,然后你在EditText中输入内容,点击Speak按钮就能把内容给朗读出来。
原文地址:http://blog.csdn.net/ichliebephone/article/details/5802044