当前位置:网站首页>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);
}
}
边栏推荐
- LM08丨网格系列之网格反转(精)
- 源代码加密的意义和措施
- [untitled]
- 力扣每日一题-第31天-202.快乐数
- Microsoft stream - how to modify video subtitles
- Vhost kick & call principle
- SQL number injection and character injection
- Discussion on several research hotspots of cvpr2022
- How outlook puts together messages with the same discussion
- ONES 创始人王颖奇对话《财富》(中文版):中国有没有优秀的软件?
猜你喜欢

软件测试方法和技术 - 基础知识概括

Day5: scanner object, next() and nextline(), sequential structure, selection structure, circular structure
![[batch DOS CMD summary] extension variables - delay variables CMD /v:on, CMD /v:off, SETLOCAL enabledelayedexpansion, disabledelayedexpansion](/img/ce/6c9e4f2c54710610e8b1f68d6d8088.png)
[batch DOS CMD summary] extension variables - delay variables CMD /v:on, CMD /v:off, SETLOCAL enabledelayedexpansion, disabledelayedexpansion

Gdip - hatchbrush pattern table

Conscience Amway universal wheel SolidWorks model material website

The triode is a great invention

Gru of RNN
![[untitled]](/img/c2/63286ba00321c9cdef43ff40635a67.png)
[untitled]

Significance and measures of source code encryption

5大组合拳,解决校园6大难题,护航教育信息化建设
随机推荐
[force deduction 10 days SQL introduction] Day9 control flow
Five combination boxing, solving six difficult problems on campus and escorting the construction of educational informatization
postgresql源码学习(26)—— Windows vscode远程调试Linux上的postgresql
【入门】截取字符串
[getting started] intercepting strings
Deep learning systematic learning
SQL number injection and character injection
038 network security JS
uni 热更新
TCP/UDP 通信问题整理
Li Kou daily question - day 31 -1502 Judge whether an arithmetic sequence can be formed
How to troubleshoot SharePoint online map network drive failure?
Kickback -- find the first palindrome character in a group of characters
Uni hot update
【批处理DOS-CMD命令-汇总和小结】-Cmd窗口中常用操作符(<、<<、&<、>、>>、&>、&、&&、||、|、()、;、@)
【入门】取近似值
web254
Aardio - 自己构造的getIconHandle的方法
Differential: definition of total differential, partial derivative, gradient
Source code analysis of open source API gateway APIs IX