当前位置:网站首页>Anddroid 文本合成语音TTS实现
Anddroid 文本合成语音TTS实现
2022-07-01 08:03:00 【林清溯】
一、近期有个人脸识别的项目中识别成功以后要有语音播报,但是三方提供的Demo中并没有此功能,于是了解TTS语音合成技术可以实现,要提前安装一个TTS引擎然后调用原生TTS,下面附上源码以及引擎apk,
AndroidManifest.xml 权限
<!--往sdcard中写入数据的权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!--往sdcard中读取数据的权限 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
二、上面是这边封装的一个工具类,使用很简单直接调用即可,这边测试正常语速可能会有点快,可以根据情况定义.
package com.baidu.idl.main.facesdk.utils;
import android.annotation.SuppressLint;
import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
public class TTSUtils {
private static final TTSUtils INSTANCE = new TTSUtils();
private TextToSpeech textToSpeak;
private boolean isSpeaking;
private TTS() {
}
/** * 播报 * * @param context * @param text 文本内容 */
public static void speak(Context context, String text) {
speak(context, text, 1.0f, 1.0f, false);
}
/** * 播报 * * @param context * @param text 文本内容 * @param ignoreIfSpeaking 如果在播报就忽略 */
public static void speak(Context context, String text, boolean ignoreIfSpeaking) {
speak(context, text, 1.0f, 1.0f, ignoreIfSpeaking);
}
/** * 播报 * * @param context * @param text 文本内容 * @param rate 语速 1.0为正常 * @param pitch 语调 1.0为正常 * @param ignoreIfSpeaking 如果在播报就忽略 */
public static void speak(Context context, String text, float rate, float pitch, boolean ignoreIfSpeaking) {
if (ignoreIfSpeaking && INSTANCE.isSpeaking) return;
INSTANCE.speakNow(context, text, rate, pitch);
}
@SuppressLint("NewApi")
private void speakNow(final Context context, final String text, final float rate, final float pitch) {
if (textToSpeak == null) {
textToSpeak = new TextToSpeech(context.getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == 0) {
speakNow(context.getApplicationContext(), text, rate, pitch);
} else {
textToSpeak = null;
}
}
});
textToSpeak.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onStart(String utteranceId) {
isSpeaking = true;
}
@Override
public void onDone(String utteranceId) {
isSpeaking = false;
}
@Override
public void onError(String utteranceId) {
isSpeaking = false;
}
});
} else {
textToSpeak.setSpeechRate(rate);
textToSpeak.setPitch(pitch);
textToSpeak.speak(text, TextToSpeech.QUEUE_FLUSH, null, "tts");
}
}
}
三、MainActivity 使用步骤
package com.litrabbit.ttsyuyin;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.litrabbit.ttsyuyin.utils.TTSUtils;
public class MainActivity extends AppCompatActivity {
/** * 记得要先安装TTS apk引擎 不然无效 项目目录下面 kdxf_tts_3.0.apk 为引擎apk * @param savedInstanceState */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/** * TextView 文本 */
TextView textView = findViewById(R.id.text_view);
/** * 播报 * * @param context * @param text 文本内容 * @param rate 语速 1.0为正常 * @param pitch 语调 1.0为正常 * @param ignoreIfSpeaking 如果在播报就忽略 */
TTSUtils.speak(MainActivity.this,textView.getText().toString(),0.5f,0.5f,true);
}
}
边栏推荐
- Caesar
- [kv260] generate chip temperature curve with xadc
- Cyclic neural network
- Basic number theory -- combinatorial number
- Insufficient executors to build thread pool
- Rk3399 platform development series explanation (network debugging) 7.30. What will affect the sending process of TCP packets?
- kubernetes资源对象介绍及常用命令(二)
- slice扩容机制分析
- 【mysql学习笔记27】存储过程
- She is the "HR of others" | ones character
猜你喜欢
![[website architecture] solve 90% of distributed transactions in one move, and introduce the working principles and application scenarios of database transactions and distributed transactions](/img/2c/07d729d49b1d74553decac4588074e.png)
[website architecture] solve 90% of distributed transactions in one move, and introduce the working principles and application scenarios of database transactions and distributed transactions

MATLAB之基础知识

Eigen matrix operation Library

【入门】输入n个整数,输出其中最小的k个

window c盘满了

SharePoint - how to quickly check whether SharePoint is standard or enterprise edition?

【入门】提取不重复的整数

How relational databases work

Access报表实现小计功能

Set up file server Minio for quick use
随机推荐
Latex table
getInputStream() has already been called for this request
[batch dos-cmd command - summary and summary] - Common operators in the CMD window (<, < <, & <,>, > >, & >, & >, & &, ||, (),;, @)
getInputStream() has already been called for this request
力扣——求一组字符中的第一个回文字符
How to use layui to display the data in the database in the form of tables
[MySQL learning notes27] stored procedure
sqlalchemy创建MySQL_Table
【力扣10天SQL入门】Day9 控制流
软键盘高度报错
[getting started] enter the integer array and sorting ID, and sort its elements in ascending or descending order
Caesar
Aardio - 阴影渐变文字
Two expressions of string
軟鍵盤高度報錯
【无标题】
【入门】提取不重复的整数
Kickback -- find the first palindrome character in a group of characters
Eigen matrix operation Library
[untitled]