使用安卓中的TextToSpeech控件实现朗读文字
首先感谢原文的博主,本文中的代码均来自该博主:(原文地址)http://flycatdeng.iteye.com/blog/1827245
朗读文字不需要任何的权限,这个控件的好处是首先不要权限,其次不用联网避免了如讯飞的联网登权限(讯飞其实也不错,比较智能,该控件只能读取简单的文字)
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.lizhanqi.www.androidtexttospeech.MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入要朗诵的字" />
<Button
android:id="@+id/btn_read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读" />
</LinearLayout>
//代码
package com.lizhanqi.www.androidtexttospeech;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Locale;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, TextToSpeech.OnInitListener {
private Button speechBtn; // 按钮控制开始朗读
private EditText speechTxt; // 需要朗读的内容
private TextToSpeech textToSpeech; // TTS对象
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
speechBtn = (Button) findViewById(R.id.btn_read);
speechBtn.setOnClickListener(this);
speechTxt = (EditText) findViewById(R.id.editText);
textToSpeech = new TextToSpeech(this, this); // 参数Context,TextToSpeech.OnInitListener
}
/**
* 用来初始化TextToSpeech引擎
* status:SUCCESS或ERROR这2个值
* setLanguage设置语言,帮助文档里面写了有22种
* TextToSpeech.LANG_MISSING_DATA:表示语言的数据丢失。
* TextToSpeech.LANG_NOT_SUPPORTED:不支持
*/
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = textToSpeech.setLanguage(Locale.CHINA);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Toast.makeText(this, "数据丢失或不支持", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onClick(View v) {
if (textToSpeech != null && !textToSpeech.isSpeaking()) {
textToSpeech.setPitch(0.0f);// 设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规
textToSpeech.speak(speechTxt.getText().toString(),
TextToSpeech.QUEUE_FLUSH, null);
}
}
@Override
protected void onStop() {
super.onStop();
textToSpeech.stop(); // 不管是否正在朗读TTS都被打断
textToSpeech.shutdown(); // 关闭,释放资源
}
}
使用安卓中的TextToSpeech控件实现朗读文字的更多相关文章
- 安卓中经常使用控件遇到问题解决方法(持续更新和发现篇幅)(在textview上加一条线、待续)
TextView设置最多显示30个字符.超过部分显示...(省略号),有人说分别设置TextView的android:signature="true",而且设置android:el ...
- CSharpGL(26)在opengl中实现控件布局/渲染文字
CSharpGL(26)在opengl中实现控件布局/渲染文字 效果图 如图所示,可以将文字.坐标轴固定在窗口的一角. 下载 CSharpGL已在GitHub开源,欢迎对OpenGL有兴趣的同学加入( ...
- 在DevExpress程序中使用SplashScreenManager控件实现启动闪屏和等待信息窗口
在我很早的WInform随笔<WinForm界面开发之"SplashScreen控件">有介绍如何使用闪屏的处理操作,不过那种是普通WInform和DevExpress ...
- 在WPF中使用WinForm控件方法
1. 首先添加对如下两个dll文件的引用:WindowsFormsIntegration.dll,System.Windows.Forms.dll. 2. 在要使用WinForm控 ...
- wpf telerik中的book控件
下载 telerik中的书本控件,仅供学习使用.
- [原创]在Framelayout中放置button控件出现的覆盖问题
android Framelayout(帧布局)是很常用的布局,主要用来处理需要多个view叠加显示的情况. 然而在使用中,我发现Framelayout中的Button控件,会挡住所有其他控件,而不论 ...
- (转)客户端触发Asp.net中服务端控件事件
第一章. Asp.net中服务端控件事件是如何触发的 Asp.net 中在客户端触发服务端事件分为两种情况: 一. WebControls中的Button 和HtmlControls中的Type为su ...
- 在web中使用windows控件,实现摄像头功能
最近做的一个Web版的视频会议项目,需要在网页中播放来自远程摄像头采集的实时视频,我们已经有了播放远程实时视频的使用C#编写的windows控件,如何将其嵌入到网页中去了?这需要使用一种古老的技术,A ...
- WPF中的image控件的Source赋值
WPF中的Image控件Source的设置 1.XAML中 简单的方式(Source="haha.png"); image控件的Source设置为相对路径后(Source=&quo ...
随机推荐
- android default_workspace.xml
//default_workspace.xml中,支持的标签有: favorite:应用程序快捷方式. shortcut:链接,如网址,本地磁盘路径等. search:搜索框. clock:桌面上的钟 ...
- mysql配置文件my.cnf解析转载
basedir = path 使用给定目录作为根目录(安装目录). character-sets-dir = path 给出存放着字符集的目录. datadir = path 从给定目录读取数据库文件 ...
- poj 2479 dp求分段最大和
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 38079 Accepted: 11904 Des ...
- 给软件增加注册功能 c#
1.软件注册类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...
- 使用apache的ab对页面进行压力测试
ab是apache自带的一个很好用的压力测试工具,当安装完apache的时候,就可以在bin下面找到ab 我们可以模拟100个并发用户,对一个页面发送1000个请求 ./ab -n1000 -c100 ...
- mysql函数date_format统计刷选按年月日统计的数据
/*原型*/ SELECT count(did) AS sum, date_format(releasetime, '%Y-%m-%d') AS releasetime FROM hengtu_dem ...
- ECSHOP商城全站自定义TITLE标题设置
对于SEO来说,能让标题自定义的将会大大增加SEO效果,提高独立商城的流量,今天小编就收集从网上弄来ecshop全站自定义代码,很全哦! 1.Ecshop商品分类页如何实现自定义Title 最近发现很 ...
- CoreData学习-最好的一片文章
CoreData学习-最好的一片文章 分类: IOS重新上路2014-05-25 18:00 1937人阅读 评论(0) 收藏 举报 目录(?)[+] 写的很好的一篇教程,我什么时候能写出这么 ...
- C语言解析日志,存储数据到伯克利DB 2
#编译程序 gcc -o historydb historydb.c -ldb #将2013年8月9日的用户记录写入数据库 (程序自动识别新用户入库,跳过老用户) ./historydb -f .us ...
- C语音--static变量
static变量大概是两种情况 在函数里的static变量意味着这个变量的生存期是全局的,你可以想象它实际上就是在函数外声明的, 当然因为可见范围的原因其他函数不能访问它 在函数外的static变量意 ...