Android提供了自动朗读支持。

  • TextToSpeech(Context context, TextToSpeech.OnInitListener listener)
  • setLanguage(Locale loc)。如果调用setLanguage(Locale loc)的返回值是 TextToSpeech.LANG_COUNTRY_AVAILABLE 则说明当前TTS系统可以支持所设置的语言、国家选项。
  • speak(String text, int queueMode, HashMap<String,String> params)
  • synthesizeToFile(String text, HashMap<String,String> params, String filename)
  • TextToSpeech.QUEUE_FLUSH
  • TextToSpeech.QUEUE_ADD


归纳起来,使用TextToSpeech引擎的步骤如下:
(1)创建TextToSpeech对象,创建时传入OnInitListener监听器监听创建是否成功。
(2)设置TextToSpeech所使用语言、国家选项,通过返回值判断TTS是否支持该语言、国家选项。
(3)调用speak或synthesizeToFile方法。
(4)关闭TTS,释放资源。
src\org\crazyit\io\Speech.java
package org.crazyit.io;import java.util.Locale;import android.app.Activity;import android.os.Bundle;import android.speech.tts.TextToSpeech;import android.speech.tts.TextToSpeech.OnInitListener;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;/** * Description: * <br/>site: <a href="http://www.crazyit.org">crazyit.org</a> * <br/>Copyright (c), 2001-2014, F.L * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author F.L@126.com * @version 1.0*/public class Speech extends Activity{TextToSpeech tts;EditText editText;Button speech;Button record;@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);// 初始化TextToSpeech对象tts = new TextToSpeech(this, new OnInitListener(){@Overridepublic void onInit(int status){// 如果装载TTS成功if ( status == TextToSpeech.SUCCESS){// 设置使用英式英语朗读int result = tts.setLanguage(Locale.UK);// 如果不支持所设置的语言if ( result != TextToSpeech.LANG_COUNTRY_AVAILABLE&& result != TextToSpeech.LANG_AVAILABLE){Toast.makeText(Speech.this, "TTS暂时不支持这种语言的朗读。", Toast.LENGTH_LONG).show();}}}});editText = (EditText) findViewById(R.id.txt);speech = (Button) findViewById(R.id.speech);record = (Button) findViewById(R.id.record);speech.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0){// 执行朗读tts.speak(editText.getText().toString(),TextToSpeech.QUEUE_ADD, null);}});record.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0){// 将朗读文本的音频记录到指定文件tts.synthesizeToFile(editText.getText().toString(), null, "/mnt/sdcard/sound.wav");Toast.makeText(Speech.this, "声音记录成功!", Toast.LENGTH_LONG).show();}});}@Overridepublic void onDestroy(){// 关闭TextToSpeec对象if ( tts != null){tts.shutdown();}}}


res\layout\main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"android:gravity="center_horizontal"><EditText android:id="@+id/txt"android:layout_width="fill_parent" android:layout_height="wrap_content" android:lines="5"/><LinearLayoutandroid:orientation="horizontal"android:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center_horizontal"><Button android:id="@+id/speech"android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/speech"/><Button android:id="@+id/record"android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/record"/></LinearLayout></LinearLayout>


res\values\strings.xml
<?xml version="1.0" encoding="utf-8"?><resources><string name="hello">Hello World, Speek!</string><string name="app_name">自动朗读</string><string name="speech">朗读</string><string name="record">记录声音</string></resources>



AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"package="org.crazyit.io"android:versionCode="1"android:versionName="1.0"><uses-sdkandroid:minSdkVersion="10"android:targetSdkVersion="17" /><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/><applicationandroid:icon="@drawable/ic_launcher"android:label="@string/app_name"><activityandroid:name=".Speech"android:label="@string/app_name"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>

更多相关文章

  1. Android之SimpleAdapter简单实例和SimpleAdapter参数说明
  2. ServiceManager Java 代理对象的获取
  3. Android(安卓)增强版HashMap,使用软引用
  4. Android(安卓)实时视频采集—Cameara预览采集
  5. get方式和post方式的请求
  6. android利用Serialization向服务器端发送任意的文件(二)
  7. Android实现自动轮播图效果
  8. Android中实现输入图片地址浏览图片操作
  9. 【Android小经验】 ListView列中的一个ITEM值

随机推荐

  1. Android仿QQ登录界面示例,实现登录、注册
  2. Android(安卓)5.0状态栏通知图标的实现
  3. (二)Unity 与 Android的布局管理
  4. Android(安卓)Studio 第八十三期 - Andro
  5. Android读取应用列表权限漏洞
  6. Android动态加载第三方APK的View研究过程
  7. Android(安卓)Studio 翻译插件Translatio
  8. Android中插件开发篇之----类加载器
  9. android 端全新指纹识别框架,适配 androi
  10. Android获取并显示图片