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

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

Main

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

main.xml

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

strings.xml

14_TTS的更多相关文章

随机推荐

  1. 【HNOI】分数分解

    题意描述 近来 IOI 专家们正在进行一项有关整数方程的研究,研究涉及到整数方程解集的统计问题,问题是这样的. 对任意的正整数 \(n\),我们有整数方程: \[\frac{1}{x_1}+\frac ...

  2. 深入IOC及其启动原理

    IOC总结 1. IOC概述 三个问题: IOC是什么 为什么用它 怎么用 1.1 是什么? 两个概念:控制反转,依赖注入 来看一下传统的干活方式:在对象单一职责原则的基础上,一个对象很少有不依赖其他 ...

  3. 《Web接口开发与自动化测试》学习笔记(三)

    一.认证系统 使用django本身自带的认证系统 1.登录admin后台 1. 先建立一个管理员用户: > python manage.py creatsuperuser 输入用户名.邮箱和密码 ...

  4. js练习题之查找数组中的位子

    输出描述: 如果数组中存在 item,则返回元素在数组中的位置,否则返回 -1 输入例子: indexOf([ 1, 2, 3, 4 ], 3) 输出例子: 2 function indexOf(ar ...

  5. SpringBoot的外部化配置最全解析!

    目录 SpringBoot中的配置解析[Externalized Configuration] 本篇要点 一.SpringBoot官方文档对于外部化配置的介绍及作用顺序 二.各种外部化配置举例 1.随 ...

  6. leetcode17gas-station

    题目描述 环形路上有n个加油站,第i个加油站的汽油量是gas[i]. 你有一辆车,车的油箱可以无限装汽油.从加油站i走到下一个加油站(i+1)花费的油量是cost[i],你从一个加油站出发,刚开始的时 ...

  7. C++ 有用的资源

    C++ 有用的资源 以下资源包含了 C++ 有关的网站.书籍和文章.请使用它们来进一步学习 C++ 的知识. C++ 有用的网站 C++ Programming Language Tutorials ...

  8. 分布式文档存储数据库之MongoDB副本集

    前文我们聊到了mongodb的索引的相关作用和介绍以及索引的管理,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/13950287.html:今天我们来聊下mon ...

  9. Pytest学习(六) - conftest.py结合接口自动化的举例使用

    一.conftest.py作用 可以理解成存放fixture的配置文件 二.conftest.py配置fixture注意事项 pytest会默认读取conftest.py里面的所有fixture co ...

  10. C++ 设计模式 4:行为型模式

    0 行为型模式 类或对象怎样交互以及怎样分配职责,这些设计模式特别关注对象之间的通信. 1 模板模式 模板模式(Template Pattern)定义:一个抽象类公开定义了执行它的方法的方式/模板.它 ...