使用TextToSpeech语音读取
来源:互联网 发布:企业阿里云邮箱登录 编辑:程序博客网 时间:2024/05/15 08:35
今天无意见发现Android中有个控件可以实现语音读取,它就是TextToSpeech控件。目前只支持5种语言:English、 French 、 German 、 Italian 和 Spanish。(很遗憾没有 中文)。这个控件的好处是首先不要权限。
TextToSpeech
必须再被实例化之后才能使用.实现TextToSpeech.OnInitListener
方法来获取实例化结果的提醒。当你已经使用完TextToSpeech
实例之后, 应该调用shutdown()
方法来释放TextToSpeech
所使用的本地资源。
内嵌的类:
class
TextToSpeech.Engine
控制文字转化语音的常亮或者参数名class
TextToSpeech.EngineInfo
已安装的语音引擎的信息interface
TextToSpeech.OnInitListener
定义了语音引擎初始化结果的回调接口interface
TextToSpeech.OnUtteranceCompletedListener
API level 18被弃用 . 使用UtteranceProgressListener
替代常量:
String
ACTION_TTS_QUEUE_PROCESSING_COMPLETED
广播事件,表示TextToSpeech
转化器已经转化未所有处于语音队列的文本int
ERROR
表示一般的操作失败int
ERROR_INVALID_REQUEST
表示由于无效请求导致的失败int
ERROR_NETWORK
表示由于网络连接问题导致的失败int
ERROR_NETWORK_TIMEOUT
表示由于网络连接超时引起的失败int
ERROR_NOT_INSTALLED_YET
表示由于未完成的语音数据下载导致的错误int
ERROR_OUTPUT
表示输出产生的失败int
ERROR_SERVICE
表示由于TTS服务产生的失败int
ERROR_SYNTHESIS
表示由于引擎的输入转化的输入内容引起的失败`intLANG_AVAILABLE
表示本地语言可用,但不是方言或者引申语言(不知道对不对)int
LANG_COUNTRY_AVAILABLE
表示本地语音或者方言可用,引申语音不可用int
LANG_COUNTRY_VAR_AVAILABLE
表示本地语音可用int
LANG_MISSING_DATA
语言包丢失int
LANG_NOT_SUPPORTED
表示语音不支持int
QUEUE_ADD
新的转化任务添加到队列后面int
QUEUE_FLUSH
新的任务替代以前的任务,直接中断以前的任务int
STOPPED
表示由代理要求的停止int
SUCCESS
操作成功构造方法:
Demo
布局文件:
activity_speech.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.testtexttospeech.TestTextToSpeechActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <EditText android:id="@+id/edview" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入内容..." android:textSize="16dp" android:layout_marginTop="20dp" android:layout_marginBottom="20dp"/> <Button android:id="@+id/speechBtn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="SpeechButton"/> </LinearLayout></RelativeLayout>代码:TestTextSpeechActivity.javapackage com.example.testtexttospeech;import android.app.Activity;import android.speech.tts.TextToSpeech;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.text.TextUtils;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import java.util.Locale;public class TestTextToSpeechActivity extends Activity implements TextToSpeech.OnInitListener{ private Button speechBtn; private EditText edview; private TextToSpeech textToSpeech; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_speech); textToSpeech = new TextToSpeech(this,this); edview = (EditText) findViewById(R.id.edview); speechBtn = (Button) findViewById(R.id.speechBtn); speechBtn.setEnabled(false); speechBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String contextStr = edview.getText().toString().trim(); if(TextUtils.isEmpty( contextStr )){ Toast.makeText(TestTextToSpeechActivity.this, "please input context",Toast.LENGTH_SHORT).show(); return; } initSpeech(contextStr); } }); } @Override public void onInit(int status) { if( status == TextToSpeech.SUCCESS){ int result = textToSpeech.setLanguage(Locale.US); if( result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED){ speechBtn.setEnabled(false); Log.e("TAG","Language is not available"); }else{ //TTS引擎已经成功初始化 speechBtn.setEnabled(true); } }else{ // 初始化失败 Log.e( "TAG", "Could not initialize TextToSpeech."); } } private void initSpeech( String contextStr ){ if( textToSpeech != null && !textToSpeech.isSpeaking()){ textToSpeech.setPitch(0.5f); // 设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规 textToSpeech.speak( contextStr ,TextToSpeech.QUEUE_FLUSH, null); } } @Override protected void onDestroy() { super.onDestroy(); if( textToSpeech != null ){ // 停止TextToSpeech textToSpeech.stop(); //释放 TextToSpeech占用的资源 textToSpeech.shutdown(); } }}
0 0
- 使用TextToSpeech语音读取
- TextToSpeech文本转为语音
- Android语音合成TextToSpeech
- TextToSpeech原生语音播报
- Android TTS 中文 文字转语音 使用TextToSpeech Svox
- Android TTS 中文 文字转语音 使用TextToSpeech Svox
- Android TTS 中文 文字转语音 使用TextToSpeech Svox .
- TTS(TextToSpeech)将文本转为语音的简单使用
- TextToSpeech将文本转为语音的简单使用
- Android中使用TTS(TextToSpeech)将文字转为语音
- 安卓文字转语音实例+代码+TextToSpeech的使用
- 使用Svox实现中文转语音TextToSpeech(TTS)
- 使用Svox实现中文转语音TextToSpeech(TTS)
- TextToSpeech将文本转为语音的简单使用
- Android TTS 英文或中文(要下载中文支持包)转语音 使用TextToSpeech
- Android联合第三方实现使用TextToSpeech实现语音朗读的功能
- TextToSpeech的使用
- TextToSpeech自动朗读使用
- Moogo数据库
- 我为什么推荐你使用 Android Studio 的 Git 工具去管理你的代码
- 保护模式
- Python学习笔记(一) 之 基础语法
- Python3实例:爬取淘宝商品列表
- 使用TextToSpeech语音读取
- 5049. 腐女的生日
- 设置bing为chrome的默认搜索引擎
- 网络协议-TCP
- 介绍Android Studio使用Git最详细的文章
- Node.js Error: listen EADDRINUSE:::
- iOS中 仿Tumblr点赞心破碎动画 韩俊强的博客
- Java调试的变迁:从System.out.println到log4j,到logback, 到slf4j(2)
- 移动页面通过javascript调取手机摄像头和图库