Android中自动朗读(TTS)的简单使用
来源:互联网 发布:手机运行电脑软件 编辑:程序博客网 时间:2024/06/10 20:16
1、 Android支持对指定文本内容进行朗读,从而发出声音,而且还支持把文本对应的音频录制成音频文件,方便以后播放。这种自动朗读支持的类为TextToSpeech,简称TTS。该类提供了如下一个构造器:TextToSpeech(Context context , TextToSpeech.OnInitListener listener),从构造器可以看出,当创建TextToSpeech对象时,必须提供了一个OnInitListener监听器,负责监听TextToSpeech的初始结果。
2、 一旦获得了TextToSpeech对象之后,就可以调用TextToSpeech的setLanguage方法来设置TTS发声引擎使用的语言、国家选项。目前Android的TTS暂时不支持中文。
3、 对TextToSpeech设置完成后,就可以朗读文本了,常用的两个方法如下:
①speak(String text,int queueMode,HashMap<String,String> params)
②synthesizeToFile(String text,HashMap<String,String>params,String filename)
上面两个方法都用于把text文字内容转换问音频,区别是speak是播放音频,synthesizeToFile是把转换得到的音频保存成声音文件。
其中,speak中的queueMode参数指定TTS发音队列模式,该参数支持如下两个常量:
4、归纳起来,使用TTS的步骤如下:
①创建TTS对象。传入的OnInitListener监听创建是否成功。
②设置TTS所使用的语言、国家选项。通过返回值判断TTS是否支持该语言、国家选项。
③调用speak或synthesizeToFile方法
④关闭TTS,回收资源。
下面用一个简单实例来演示TTS的使用,代码如下:
Activity:
package com.lovo.speech;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.widget.EditText;import android.widget.Toast;public class SpeechTestActivity extends Activity {private TextToSpeech tts;private EditText editText;@Overrideprotected 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.US);// 如果不支持所设置的语言if (result != TextToSpeech.LANG_COUNTRY_AVAILABLE&& result != TextToSpeech.LANG_AVAILABLE) {Toast.makeText(SpeechTestActivity.this,"TTS暂时不支持这种语言的朗读!", Toast.LENGTH_LONG).show();}}}});editText = (EditText) findViewById(R.id.main_et_content);}@Overrideprotected void onDestroy() {super.onDestroy();// 关闭TextToSpeech对象if (tts != null) {tts.shutdown();}}public void click(View v) {switch (v.getId()) {case R.id.main_btn_record:// 将朗读文本的音频记录到指定文件tts.synthesizeToFile(editText.getText().toString(), null,"/mnt/sdcard/sound.wav");Toast.makeText(SpeechTestActivity.this, "声音记录成功!",Toast.LENGTH_SHORT).show();break;case R.id.main_btn_speech:// 执行朗读tts.speak(editText.getText().toString(), TextToSpeech.QUEUE_ADD,null);break;}}}
布局XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/main_et_content" android:layout_width="match_parent" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="horizontal" > <Button android:id="@+id/main_btn_speech" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click" android:text="朗读" /> <Button android:id="@+id/main_btn_record" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:onClick="click" android:text="记录声音" /> </LinearLayout></LinearLayout>
- Android中自动朗读(TTS)的简单使用
- Android自动朗读(TTS)的实现
- Android自动朗读(TTS)的实现
- Android 自动朗读(TTS)
- Android中的自动朗读(TTS)
- Android自动朗读(TTS)
- Andorid自动朗读TTS使用
- Android学习笔记(4)---自动朗读TTS发音使用
- Android的数据存储和IO - 自动朗读(TTS)
- 初学Android,自动朗读TTS(五十二)
- 自动朗读(TTS)
- android 中文朗读 语音朗读 tts朗读
- 安卓自动朗读TTS
- 使用VB.net 创建简单的TTS中英文混合语音朗读
- 使用VB.net 创建简单的TTS中英文混合语音朗读
- 在VB中使用文字朗读引擎(TTS)技术
- 在VB中使用文字朗读引擎(TTS)技术
- Android TTS学习 连续朗读
- 不要害怕,付出了,没有收获!
- vc显示包含的库/verbose:lib
- 我要学编程,看什么书好?--^_^,这里推荐一些个人觉得很不错的书(五)小集篇
- sql语句中的逻辑运算:或、与、异或
- html5基础的常用的技巧
- Android中自动朗读(TTS)的简单使用
- WPF中利用WebClient向服务器上传文件
- 数据库字段命名,你是否也傻眼?
- 读取的XML节点中带有冒号怎么办?
- 缓存
- 如何使用cocos2d制作一个太空射击游戏
- logback配置文件
- vlc-android编译问题之live555
- 面试算法题:忘我之乘积分析