自动朗读(TTS)
来源:互联网 发布:纬创软件 福州 编辑:程序博客网 时间:2024/06/11 07:15
使用TextToSpeech 的步骤如下:
- 创建 TextToSpeech 对象,创建传入时的 TextToSpeech.OnInitListener() 监听器监听创建是否成功.
- 设置 TextToSpeech 所使用的语言,国家选项,通过返回值判断TTS是否支持该语言, 国家选项.
- 调用 speak() 或者 synthesizeToFile()方法.
- 关闭 TTS,释放资源.
package com.test.tts;import android.app.Activity;import android.os.Bundle;import android.speech.tts.TextToSpeech;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import java.util.HashMap;import java.util.Locale;import java.util.Map;/** * 自动朗读 */public class SpeechActivity extends Activity { TextToSpeech tts; EditText etText; Button btnSpeech, btnRecord; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_speech); //第一步 初始化 TextToSpeech对象 tts = new TextToSpeech(this, new TextToSpeech.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(SpeechActivity.this, "TTS暂时不支持这种语言的朗读", Toast.LENGTH_SHORT).show(); } } } }); etText = (EditText) findViewById(R.id.et_text); btnSpeech = (Button) findViewById(R.id.speech); btnRecord = (Button) findViewById(R.id.record); //朗读事件监听 btnSpeech.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //执行朗读 tts.speak(etText.getText().toString(), TextToSpeech.QUEUE_ADD, null); } }); //记录监听事件 btnRecord.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (tts != null && etText != null) { Map<String,String> map = new HashMap<String, String>(); //这里的map只是笔者自己加的 防止报错 //将朗读文本的音频记录到指定文件 tts.synthesizeToFile(etText.getText().toString(), (HashMap<String, String>) map, "/mnt/sdcard/sound.wav"); } Toast.makeText(SpeechActivity.this, "声音记录成功", Toast.LENGTH_SHORT).show(); } }); } @Override protected void onDestroy() { super.onDestroy(); //关闭 TextToSpeech 对象 if (tts != null) { tts.shutdown(); } }}
布局界面
<?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.test.tts.SpeechActivity"> <EditText android:id="@+id/et_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入文字进行朗读" android:lines="3"/> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="horizontal"> <Button android:id="@+id/speech" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="朗读" /> <Button android:id="@+id/record" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="记录" /> </LinearLayout></LinearLayout>
0 0
- 自动朗读(TTS)
- Android 自动朗读(TTS)
- Android中的自动朗读(TTS)
- Android自动朗读(TTS)
- Andorid自动朗读TTS使用
- 安卓自动朗读TTS
- 初学Android,自动朗读TTS(五十二)
- Android自动朗读(TTS)的实现
- Android自动朗读(TTS)的实现
- Android的数据存储和IO - 自动朗读(TTS)
- Android学习笔记(4)---自动朗读TTS发音使用
- Android中自动朗读(TTS)的简单使用
- android 中文朗读 语音朗读 tts朗读
- TTS语音朗读
- TTS语音朗读
- QT TTS朗读
- TTS实现文字语音朗读
- Android TTS学习 连续朗读
- 什么是资本运营的核心?
- Swift 语言数据类型
- ACdream 1213 简单数学
- 屏幕旋转
- Android快速开发之设计通用的ListView,GridView的适配器
- 自动朗读(TTS)
- No mapping found for HTTP request with URI [/operation-platform/js/displaytag.js] in DispatcherServl
- 圆形背景TextView
- Visual Studio 2013 Professional Key
- R应用-多元线性回归
- 考取PMP的价值何在?
- java设计模式之代理模式
- JAVA 对象拷贝
- Behavior of Active Dataguard(ADG) When Apply Node Aborts/Crash (文档 ID 1613719.1)