android学习笔记53——自动朗读TTS
自动朗读TTS
android提供了自动朗读功能——其指的是支持可以对指定文本内容进行朗读,从而发出声音;
同时android的自动朗读支持还允许把文本对应的音频录制成音频文件,方便后续播放。
这种自动朗读支持的英文名称为:TextToSpeech,检测TTS.
TTS,可以在应用程序中动态地增加音频输出,从而提高用户体验。
Android的自动朗读支持通过TextToSpeech完成,该类提供了如下一个构造函数:
==>TextToSpeeech(Content content,TextToSpeeck.OnInitListener listener),当创建TextToSpeech对象时,必须先提供一个OnInitListener监听器——该监听器负责监听TextToSpeech的初始化效果。
程序获取TextToSpeeech对象后,可调用TextToSpeeech的setLanguage(Locale loc)方法设置该TTS发声引擎使用的语音、国家选项。
注意:
如果调用setLanguage(Locale loc)的返回值是:TextToSpeech.LANG_COUNTRY_AVALABLE,说明当前TTS系统可以支持所设置的语音、国家选项。
对TextToSpeech设置完成后,即可调用它的方法来朗读文本了,具体方法可参考TextToSpeech的API文档。
TextToSpeech类中最常用的两个方法如下:
1.speak(String text,int queueMode,HashMap<String,String> params);
2.synthesizeToFile(String text,HashMap<String,String> params,String fileName);
以上两个方法都是用于把text文字内容转换为音频,区别只是speak方法是播放转换的音频,而synthesizeToFile是把转换得到的音频保存成声音文件。
以上两个方法中的params都用于指定声音转换时的参数,speak()中的queueMode参数指定TTS的发音队列模式,该模式支持如下两个常量:
1.TextToSpeech.QUEUE_PLUSH:如果指定该模式,当TTS调用speak()时,它会中断当前实例正在运行的任务(也可理解为清除当前语音任务,转而执行新的语音任务)
2.TextToSpeech.QUEUE_ADD:如果指定该模式,当TTS调用speak()时,会把新的发音任务添加到当前发音任务列队之后——也就是等任务队列中的发音任务执行完成后再来执行speak()指定的发音任务。
注意:
当程序用完了TextToSpeech对象后,可以在Activity的OnDestory()中调用它的shutdown()来关闭TextToSpeech、释放其所占用资源。
总结:TextToSpeech使用步骤如下:
1.创建TextToSpeech对象,创建时传入OnInitListener监听器监听创建是否成功;
2.设置TextToSpeech所使用语言、国家选项,通过返回值判断TTS是否支持该语言、国家选项;
3.调用speak()或synthesizeToFile();
4.关闭TTS,回收资源。
实例如下:
布局文件==》
<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=".MainActivity" > <EditText
android:id="@+id/edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this is a dog" /> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" > <Button
android:id="@+id/btnRecord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记录声音" /> <Button
android:id="@+id/btnRead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="朗读" />
</LinearLayout> </LinearLayout> 代码实现==>
package com.example.mytts; import java.util.Locale; import android.os.Bundle;
import android.app.Activity;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends Activity
{
EditText edit;
TextToSpeech tts; @Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); edit = (EditText) this.findViewById(R.id.edit);
Button btnRecord = (Button) this.findViewById(R.id.btnRecord);
Button btnRead = (Button) this.findViewById(R.id.btnRead); btnRecord.setOnClickListener(new MyButtonOnClick());
btnRead.setOnClickListener(new MyButtonOnClick()); tts = new TextToSpeech(this, new OnInitListener()
{
@Override
public void onInit(int status)
{
// 如果装载TTS引擎成功
if (status == TextToSpeech.SUCCESS)
{
// 设置使用美式英语朗读
int result = tts.setLanguage(Locale.US);
// 如果不支持所设置的语言、国家
if (result != TextToSpeech.LANG_COUNTRY_AVAILABLE
&& result != TextToSpeech.LANG_AVAILABLE)
{
Toast.makeText(MainActivity.this, "TTS不支持本次设置语言、国家的朗读", 5000).show();
}
}
}
});
} private class MyButtonOnClick implements OnClickListener
{
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.btnRecord:
// 将朗读文本的音频记录到指定文件
tts.synthesizeToFile(edit.getText().toString(), null, "/mnt/sdcard/sound.wav");
Toast.makeText(MainActivity.this, "声音记录成功", 5000).show();
break;
case R.id.btnRead:
tts.speak(edit.getText().toString(), TextToSpeech.QUEUE_ADD, null);
break;
}
} } @Override
protected void onDestroy()
{
// super.onDestroy();
if (tts != null)
tts.shutdown();
} @Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
android学习笔记53——自动朗读TTS的更多相关文章
- Android 学习笔记之Volley(七)实现Json数据加载和解析...
学习内容: 1.使用Volley实现异步加载Json数据... Volley的第二大请求就是通过发送请求异步实现Json数据信息的加载,加载Json数据有两种方式,一种是通过获取Json对象,然后 ...
- Android学习笔记36:使用SQLite方式存储数据
在Android中一共提供了5种数据存储方式,分别为: (1)Files:通过FileInputStream和FileOutputStream对文件进行操作.具体使用方法可以参阅博文<Andro ...
- 【转】 Pro Android学习笔记(七八):服务(3):远程服务:AIDL文件
目录(?)[-] 在AIDL中定义服务接口 根据AIDL文件自动生成接口代码 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.n ...
- 【转】 Pro Android学习笔记(七十):HTTP服务(4):SOAP/JSON/XML、异常
目录(?)[-] SOAP JSON和XMLPullParser Exception处理 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件,转载须注明出处:http://blog. ...
- 【转】 Pro Android学习笔记(六七):HTTP服务(1):HTTP GET
目录(?)[-] HTTP GET小例子 简单小例子 出现异常NetworkOnMainThreadException 通过StrictMode进行处理 URL带键值对 Andriod应用可利用ser ...
- 【转】 Pro Android学习笔记(五六):配置变化
目录(?)[-] Activity的destorycreate过程 Fragment的destorycreate过程 onSaveInstanceState saveFragmentInstanceS ...
- 【转】 Pro Android学习笔记(四十):Fragment(5):适应不同屏幕或排版
目录(?)[-] 设置横排和竖排的不同排版风格 改写代码 对于fragment,经常涉及不同屏幕尺寸和不同的排版风格.我们在基础小例子上做一下改动,在横排的时候,仍是现实左右两个fragment,在竖 ...
- 【转】 Pro Android学习笔记(三三):Menu(4):Alternative菜单
目录(?)[-] 什么是Alternative menu替代菜单 小例子说明 Alternative menu代码 关于Category和规范代码写法 关于flags 多个匹配的itemId等参数 什 ...
- 【转】Pro Android学习笔记(三十):Menu(1):了解Menu
目录(?)[-] 创建Menu MenuItem的属性itemId MenuItem的属性groupId MenuItem的属性orderId MenuItem的属性可选属性 Menu触发 onOpt ...
随机推荐
- [hadoop] 一些基础概念
一.云的概念 1.云计算的概念 随时 随地 使用任何设备 获得任何服务 2.趋势 )资料开始回归集中处理(存储大量资料) 随时存取 降低遗失风险 减少传输成本 促进团队协作 )网页变为预设开发平台(网 ...
- cookie 二:
本篇随笔从cookie的入门开始,介绍了cookie的设置获取和移除,还有一些小的应用案例:一.设置cookie <script> //设置cookie:function setCooki ...
- App压力测试整理
压力测试结果:CRASH:崩溃,应用程序在使用过程中,非正常退出ANR:Application Not Responding MonkeyRunner APIs MonkeyRunner:用来连接设备 ...
- C#压缩文件夹
using System;using System.Collections.Generic;using System.Text; ///第三方dllusing ICSharpCode.SharpZip ...
- ets查询:查询表中的具体一列的所有值
比如要查询goods表中的ID这一列的所有值: P = [{#goods{upgrade='$1',_ = '_'},[],['$1']}] 要查询ID和Upgrade这两列的值: P2 = [{#g ...
- PHP 函数(2)
自定义函数: $name = "fakeface"; function dispalyName(){ echo "fakeface"; } function r ...
- 【转】request和response的页面跳转传参
下面是一位园友的文章: jsp或Servlet都会用到页面跳转,可以用 request.getRequestDispatcher("p3.jsp").forward(request ...
- eclipse 执行out.request()方法提示out cannot be resolved
添加代码:PrintWriter out = response.getWriter(); 支持中文:response.setContentType("text/html;charset=ut ...
- 【matlab】将matlab中数据输出保存为txt或dat格式
将matlab中数据输出保存为txt或dat格式 总结网上各大论坛,主要有三种方法. 第一种方法:save(最简单基本的) 具体的命令是:用save *.txt -ascii x x为变量 *.txt ...
- sqoop连接oracle与mysql&mariadb的错误
错误说明: 由于我的hadoop的集群是用cloudera manager在线自动安装的,因此他们的安装路径必须遵循cloudera的规则,这里只有查看cloudera的官方文档了,请参考:http: ...