TTS(Text to speech)为语音合成的意思。本课程主要介绍了TTS的使用方法。

 1 package cn.eoe.tts;
2
3 import java.util.Locale;
4 import android.annotation.SuppressLint;
5 import android.app.Activity;
6 import android.os.Bundle;
7 import android.speech.tts.TextToSpeech;
8 import android.view.View;
9 import android.view.View.OnClickListener;
10 import android.widget.Button;
11 import android.widget.TextView;
12 import android.widget.Toast;
13
14 @SuppressLint("NewApi") public class Main extends Activity implements TextToSpeech.OnInitListener,
15 OnClickListener {
16 private TextToSpeech tts;
17 private TextView textView;
18
19 @Override
20 public void onCreate(Bundle savedInstanceState) {
21 super.onCreate(savedInstanceState);
22 setContentView(R.layout.main);
23 tts = new TextToSpeech(this, this);
24
25 Button button = (Button) findViewById(R.id.button);
26 textView = (TextView) findViewById(R.id.textview);
27 button.setOnClickListener(this);
28 }
29
30 public void onClick(View view) {
31 tts.speak(textView.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
32 }
33
34 @Override
35 public void onInit(int status) {
36 if (status == TextToSpeech.SUCCESS) {
37 int result = tts.setLanguage(Locale.US);
38 if (result == TextToSpeech.LANG_MISSING_DATA
39 || result == TextToSpeech.LANG_NOT_SUPPORTED) {
40 Toast.makeText(this, "Language is not available.",
41 Toast.LENGTH_LONG).show();
42 }
43 }
44
45 }
46
47 }

Main

 1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" >
6
7 <Button
8 android:id="@+id/button"
9 android:layout_width="fill_parent"
10 android:layout_height="wrap_content"
11 android:text="说话" />
12
13 <TextView
14 android:id="@+id/textview"
15 android:layout_width="fill_parent"
16 android:layout_height="fill_parent"
17 android:text="@string/text" />
18
19 </LinearLayout>

main.xml

 1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3 <string name="hello">Hello World, Main!</string>
4 <string name="app_name">朗读文本</string>
5 <string name="text">Android 2.1 is a minor platform release deployable
6 to Android-powered handsets starting in January 2010. This release
7 includes new API changes and bug fixes. For information on changes,
8 see the Framework API section.\n
9
10 For developers, the Android 2.1 platform is available as a downloadable
11 component for the Android SDK. The downloadable platform includes a
12 fully compliant Android library and system image, as well as a set of
13 emulator skins, sample applications, and more. The downloadable
14 platform includes no external libraries.\n
15
16 To get started developing or testing against the Android 2.1 platform,
17 use the Android SDK and AVD Manager tool to download the platform into
18 your Android SDK. For more information, see Adding SDK Components.
19 </string>
20 </resources>

strings.xml

14_TTS的更多相关文章

随机推荐

  1. OpenCascade拓扑对象之:拓扑对象的层次结构

    @font-face { font-family: "Times New Roman" } @font-face { font-family: "宋体" } @ ...

  2. python之冒泡排序改进

    冒泡排序改进 关注公众号"轻松学编程"了解更多. 一.普通冒泡排序 [22,3,1,6,7,8,2,5] 普通冒泡排序 思路: 第一趟排序 从下标0开始,取出对应的值22 22和3 ...

  3. python机器学习实现逻辑斯蒂回归

    逻辑斯蒂回归 关注公众号"轻松学编程"了解更多. [关键词]Logistics函数,最大似然估计,梯度下降法 1.Logistics回归的原理 利用Logistics回归进行分类的 ...

  4. 已经编译安装的nginx/tenginx编译增加新模块

    只适用于自行编译安装的nginx配置 业务变更带来的Nginx增加模块需求 由于业务从php转为go开发,需要用到Http2的协议.这种协议在Nginx上需要http_v2_module这个模块的支持 ...

  5. svg究竟是什么?

    svg究竟是什么? 1 要点 要点1:svg与jpg/png等格式的用途完全不同,不可相提并论,没有可比性,不可互相替代. 要点2:日常生活中,我们用相机拍摄自然景象得到的照片和视频,能且只能用jpg ...

  6. dedecms织梦手机端文章内容页图片不能自适应解决方法

    dedecms织梦手机端文章内容页图片不能自适应解决方法: 方法一修改手机端文章页模板代码: 找到并打开手机端的文章内容页模板,将里面的{dede:field.body/}标签修改一下,改为如下的标签 ...

  7. 你真的会用PostGIS中的buffer缓冲吗?

    buffer - 图形缓冲区分析,GIS中最基本的空间分析之一. 实现buffer的工具有很多种,例如前端的truf.js.服务端的ArcGISserver.桌面端的ArcMap.数据库端的PosrG ...

  8. 如何用css设置鼠标属性的小手

    在元素中添加onmouseover="this.style.cursor='hand'"或者 cursor:pointer;

  9. waf 引擎 云原生平台tproxy 实现调研

    了解了基本 云原生架构,不清楚的查看之前的文章:https://www.cnblogs.com/codestack/p/13914134.html 现在来看看云原生平台tproxy waf引擎串联实现 ...

  10. shell编程之条件与分支语句

    1.if条件分支语句 if   expr1(条件测试)   #如果expr1为真,返回0 then commands1 elif  expr2 then commands2 .... ... else ...