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

最新文章

  1. CSS 重设文章
  2. 1.Windows安装PostgreSQL
  3. T4教程2 T4模版引擎之生成数据库实体类
  4. JAVA帮助文档全系列 JDK1.5 JDK1.6 JDK1.7 官方中英完整版下载
  5. 视频转换工具 Transmageddon
  6. .Net 接连 Oracle 数据库(Winform)
  7. qt 设置背景图片
  8. sql server 2008 r2 清除数据库日志
  9. python学习随笔
  10. Android的应用程序的异常处理2
  11. 淘特房产CMS系统 7.5
  12. Arrays工具类十大常用方法
  13. 史上最全的java随机数生成算法[转载]
  14. thinkphp5和thinkphp3.2.3中URL重写出现No input file specified
  15. RDMA RC UC UD
  16. CodeForces165E 位运算 贪心 + 状压dp
  17. JS自学笔记02
  18. laravel----------php7.0.12 laravel 链接sqlserver数据库
  19. MQ问题
  20. fatal error C1859 意外的预编译头错误,只需重新运行编译器(转)

热门文章

  1. xadmin开发后台管理系统常见问题
  2. c++ 从vector扩容看noexcept应用场景
  3. 《精通Spring4.x企业应用开发实战》第三章
  4. C#设计模式-建造者模式(Builder Pattern)
  5. 2012年游戏软件开发独立本科段01B0815自考科目教材
  6. 使用 .NET 5 体验大数据和机器学习
  7. CSS两列布局的多种方式
  8. Difference between skbuff frags and frag_list
  9. linux 信号 ctrl + d z c fg bg 作用
  10. Java基础 之二 类和对象