tts文字转语音

In this tutorial, we’ll be discussing and implementing the Text To Speech in our Android application.
We will create an android application which speaks the text entered in the EditText. Android Text To Speech is also referred to as Android TTS.

在本教程中,我们将在Android应用程序中讨论和实现“文字转语音”。
我们将创建一个Android应用程序,说出在EditText中输入的文本。 Android文字转语音也称为Android TTS。

Android文字转语音 (Android Text To Speech)

TextToSpeech as the name suggests is used to synthesize speech from the text string.

顾名思义,TextToSpeech用于从文本字符串合成语音。

You can set the language of your choice. You can set the pitch, speed as well your own speech from a custom file.

您可以设置自己选择的语言。 您可以从自定义文件中设置音高,速度以及自己的语音。

TextToSpeech needs to be initialized first. For this, you need to implement the TextToSpeech.OnInitListener interface and override the method: onInit.
Example:

首先需要初始化TextToSpeech。 为此,您需要实现TextToSpeech.OnInitListener接口并覆盖方法: onInit
例:

TextToSpeech tts = new TextToSpeech(this, this);

Once this is done the onInit gets triggered.

一旦完成此操作,就会触发onInit。

In this method, we check whether the feature is available on our device or not.

通过这种方法,我们检查功能是否在我们的设备上可用。

@Overridepublic void onInit(int status) {if (status == TextToSpeech.SUCCESS) {int result = tts.setLanguage(Locale.US);if (result == TextToSpeech.LANG_MISSING_DATA|| result == TextToSpeech.LANG_NOT_SUPPORTED) {Toast.makeText(getApplicationContext(), "Language not supported", Toast.LENGTH_SHORT).show();} else {//Disable the button if any.}} else {Toast.makeText(getApplicationContext(), "Init failed", Toast.LENGTH_SHORT).show();}}

How to speak the text?

文字怎么说?

tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, null);

The first parameter is the text that would be spoken.

第一个参数是要说的文字。

The second parameter defines that the previous input is flushed so as to begin a clean slate. Alternatively, we can use QUEUE_ADD to add the current text to the speech.

第二个参数定义刷新先前的输入,以便开始清理。 或者,我们可以使用QUEUE_ADD将当前文本添加到语音中。

The third parameter is the bundle that is passed.

第三个参数是传递的包。

Fourth is the utterance id string.

第四是utterance id字符串。

OnUtteranceProgressListener is used to listen for the tts events: start, done, error.

OnUtteranceProgressListener用于侦听tts事件:开始,完成,错误。

In this, we can retrieve the bundle utterance string that was passed.

在这种情况下,我们可以检索已传递的语音包发声字符串。

The OnUtteranceProgressListener must be defined before the speak method is called.
必须在调用语音方法之前定义OnUtteranceProgressListener。

To change the pitch and speed we can do:

要更改音调和速度,我们可以执行以下操作:

tts.setPitch(2f);
tts.setSpeechRate(2f);

addSpeech method is used to add a custom speech instead of using the Android’s default speech.

addSpeech方法用于添加自定义语音,而不是使用Android的默认语音。

Once the speech is over, you need to stop the TTS instance in order to release the resources using the tts.shutdown() method.

演讲结束后,您需要停止TTS实例,以便使用tts.shutdown()方法释放资源。

Let’s get onto the business end where we implement TextToSpeech in our application.

让我们进入在应用程序中实现TextToSpeech的业务端。

Android TTS项目结构 (Android TTS Project Structure)

文字转语音Android代码 (Text To Speech Android Code)

The code for the activity_main.xml layout is given below:

下面给出了activity_main.xml布局的代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="https://schemas.android.com/apk/res/android"xmlns:app="https://schemas.android.com/apk/res-auto"xmlns:tools="https://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><EditTextandroid:id="@+id/enterYouText"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="8dp"android:text="Welcome to JournalDev.com"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="8dp"android:text="SPEAK TEXT"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/enterYouText" /><Buttonandroid:id="@+id/btnChangePitch"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginEnd="8dp"android:layout_marginStart="8dp"android:layout_marginTop="8dp"android:text="Change Pitch"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/btnChangeSpeed" /><Buttonandroid:id="@+id/btnChangeSpeed"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginEnd="8dp"android:layout_marginStart="8dp"android:layout_marginTop="8dp"android:text="Change Speed"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/button" /></android.support.constraint.ConstraintLayout>

The code for MainActivity.java is given below:

MainActivity.java的代码如下:

package com.journaldev.androidtexttospeech;import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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 TextToSpeech.OnInitListener {Button button, btnChangePitch, btnChangeSpeed;EditText editText;private TextToSpeech tts;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tts = new TextToSpeech(this, this);button = findViewById(R.id.button);btnChangePitch = findViewById(R.id.btnChangePitch);btnChangeSpeed = findViewById(R.id.btnChangeSpeed);editText = findViewById(R.id.enterYouText);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {speakOut();}});btnChangePitch.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {tts.setPitch(0.2f);}});btnChangeSpeed.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {tts.setSpeechRate(2f);}});}@Overridepublic void onInit(int status) {if (status == TextToSpeech.SUCCESS) {int result = tts.setLanguage(Locale.US);if (result == TextToSpeech.LANG_MISSING_DATA|| result == TextToSpeech.LANG_NOT_SUPPORTED) {Toast.makeText(getApplicationContext(), "Language not supported", Toast.LENGTH_SHORT).show();} else {button.setEnabled(true);}} else {Toast.makeText(getApplicationContext(), "Init failed", Toast.LENGTH_SHORT).show();}}private void speakOut() {tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {@Overridepublic void onStart(String s) {final String keyword = s;runOnUiThread(new Runnable() {@Overridepublic void run() {Toast.makeText(getApplicationContext(), "Started" + keyword, Toast.LENGTH_SHORT).show();}});}@Overridepublic void onDone(String s) {runOnUiThread(new Runnable() {@Overridepublic void run() {Toast.makeText(getApplicationContext(), "Done ", Toast.LENGTH_SHORT).show();}});}@Overridepublic void onError(String s) {runOnUiThread(new Runnable() {@Overridepublic void run() {Toast.makeText(getApplicationContext(), "Error ", Toast.LENGTH_SHORT).show();}});}});Bundle params = new Bundle();params.putString(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "");String text = editText.getText().toString();tts.speak(text, TextToSpeech.QUEUE_FLUSH, params, "Dummy String");}@Overridepublic void onDestroy() {if (tts != null) {tts.stop();tts.shutdown();}super.onDestroy();}
}
runOnUiThread function.runOnUiThread函数。

The output of the above application in action is given below:

上面应用程序的输出如下:

Try pressing the pitch and speech and notice the change!

尝试按音调和语音并注意变化!

This brings an end to this tutorial. You can download the project from the link below:

本教程到此结束。 您可以从下面的链接下载项目:

AndroidTextToSpeechAndroidTextToSpeech
Github Project LinkGithub项目链接

翻译自: https://www.journaldev.com/21904/android-text-to-speech-tts

tts文字转语音

tts文字转语音_Android文字转语音(TTS)相关推荐

  1. 【PC工具】更新文字转语音、文字文本朗读工具,语音朗读软件,TTS语音合成...

    公众号关注 "DLGG创客DIY" 设为"星标",重磅干货,第一时间送达. 昨天在群里嫖到一个在线的文字转语音(TTS)工具,不敢独享拿来分享.上次分享的文字转 ...

  2. HTML5文字转语音源码,微软TTS语音源码(将文本转为语音并播放)

    [实例简介]利用微软TTS语音,字符串转语音播放,或者保存为语音文件. 语音库需自行下载,推荐Hui 发音人 微软TTS文字转语音发音人修复 微软TTS语音 Win7修复 发音人 [实例截图] [核心 ...

  3. TTS 文字转语音 语音转文字

    pyttsx3 pip install pyttsx3import pyttsx3def say_text(engine, words, voice):# 设置音色engine.setProperty ...

  4. Python 神工具包!翻译、文字识别、语音转文字统统搞定

    今天给大家介绍一款 Python 制作的实用工具包,包含多种功能: 音频转文字 文字转语音 截图 OCR文字识别 复制翻译 举个例子,比如截图 OCR 文字识别就有很多实用场景. 常会遇到有些 PDF ...

  5. python识别pdf文字_Python 神工具包!翻译、文字识别、语音转文字统统搞定

    今天给大家介绍一款 Python 制作的实用工具包,包含多种功能: 音频转文字 文字转语音 截图 OCR文字识别 复制翻译 举个例子,比如截图 OCR 文字识别就有很多实用场景. 常会遇到有些 PDF ...

  6. Android百度语音集成——文字转语音

    项目涉及文字转语音的需求,用Android原生提供的TTS生成的语音太单调,机器声音太明显,故寻求第三方更好的支持,用科大讯飞的语音包收费,百度语音免费而且不限制调用次数,主页鲜明说永久免费的智能语音 ...

  7. Qt5 WindonwsTTS语音朗读 文字 朗读

    Qt5 WindowsTTS语音朗读 Qt之WindowsTTS语音朗读 文章目录 Qt5 WindowsTTS语音朗读 一.添加pro 二.使用步骤 1.引入库 2.文件.h 3.文件.cpp 总结 ...

  8. Qt --实现语音读文字功能

    目的:实现语音读文字功能 .h #ifndef MAINWINDOW_H #define MAINWINDOW_H#include <QMainWindow> #include <Q ...

  9. python语音转文字_Python文字转语音示例

    python语音转文字 Here you will get python text to speech example. 在这里,您将获得python文本语音转换示例. As we know, som ...

最新文章

  1. Java将视频转为缩略图--ffmpeg
  2. Floyd cycle算法
  3. 施耐德电气:2016年数据中心的三大关注领域
  4. 再次强调事件绑定中this的坑
  5. 金山笔试题-字符串排序 : 写一个函数,实现对给定的字符串(字符串里面包括:英文字母,数字,符号)的处理...
  6. appcan slider轮播图和页面弹动冲突解决
  7. 前端JavaScript之BOM与DOM
  8. 使用MATLAB绘制周期信号的,周期信号频域分析及MATLAB实现.ppt
  9. bus Hound使用详解
  10. 三菱plc分拣程序_基于三菱PLC与视觉检测的快速分拣控制系统
  11. selenium与firefox、 chrome版本对应关系
  12. 台式机就是指什么的计算机,什么是台式机操作系统
  13. 谈论机器学习中,哪种学习算法更好有意义吗?
  14. 用switch语句根据消费金额计算折扣 (Java经典编程案例)
  15. 安卓海外SDK接入问题
  16. 生产者/消费者模式之深入理解
  17. 超级签名-原理/机制/技术细节-完全解析
  18. stack overflow -最好的编程技术论坛!
  19. MATLAB求解方程和多元方程组
  20. 李铁:智慧城市发展存在一个严重误区

热门文章

  1. python脚本性能分析
  2. 【洛谷3157】[CQOI2011] 动态逆序对(CDQ分治)
  3. Chapter 1: 使用引用类型
  4. Java数据结构(1)---顺序表
  5. ubuntu14.04+CUDA7.5+cuDNN+caffe的超详细完整配置
  6. 树莓派系统的安装、初步配置与远程访问
  7. 图像匹配得到精确的旋转角度
  8. IMU-Allan方差分析
  9. python linkedlist,LinkedList在python中的实现
  10. php ext_skel,用ext_skel为php开发扩展|待更