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. js——事件循环

    JS-事件循环 js运行的环境称之为宿主环境. 执行栈 :call stack ,一个数据结构,用于存放各种函数的执行环境,每一个函数执行之前他的相关信息会加入到执行栈中,函数调用之前,创建执行环境, ...

  2. iframe框架下的某一页面跳转到另外一个页面

    //iframe只在当前页跳转 window.location = "/Admin/Blog/Index"; 只在当前页跳转 而不是整个页面都跳转

  3. Css gray 无法覆盖IE10

    网站变灰这个效果很常见,在我这里暂时没有找到最优解决方式, 先把今天的研究结果记录一下. 第一种方案 : 对所有静态资源文件进行灰度处理,得到新一个资源目录,例如asset_ori 原始资源    a ...

  4. Vue2.0源码分析

    Vue其实是用Function写的一个class: 1.通过一系列的函数给Vue.prototype上动态挂载方法和属性 2.通过initGlobalAPI(Vue)给Vue本身扩展全局API 数据驱 ...

  5. vue-count-to(简单好用的一个数字滚动插件)

    vue-count-to是一个无依赖,轻量级的vue组件,可覆盖easingFn. 1. 你可以设置两个属性startVal和endVal,它会自动判断计数或倒计时.支持vue-ssr.vue-cou ...

  6. solr 笔记

    1.sorl其实是对存储的内容,根据相应的域和域的类型先分词,停顿,过滤(大小写转换)等等;然后建立多级索引.对搜索条件也是根据相应的域和域的类型进行分词,停顿,同义词,过滤(大小写转换)等等;然后建 ...

  7. linux centos 6.x 装机后基本优化

    1.关闭SELinux /etc/selinux/config配置文件内替换 se -i 's/SELINUX=enforcing/SELINUX=disabled/g'需要重启grep SELINU ...

  8. List、Tuple、Set、Dictionary数据类型

    一.List数据类型 1.概述:list(列表)中可以包含多个元素,且元素类型可以不相同. 每一元素可以是任意数据类型,包括列表(即列表嵌套)及后面要介绍的元组.集合.字典. 所有元素都写在一对方括号 ...

  9. kali 更新msf

    用leafpad打开,方便复制粘贴 leafpad /etc/apt/sources.list 然后复制下面的源覆盖原本的 deb http://mirrors.ustc.edu.cn/kali ka ...

  10. 实验吧[WEB]——what a fuck!这是什么鬼东西?

    解题链接:http://ctf5.shiyanbar.com/DUTCTF/1.html 原题链接:http://www.shiyanbar.com/ctf/56 解题必看: 的jother编码定义: ...