当前位置:网站首页>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);
}
}
边栏推荐
- getInputStream() has already been called for this request
- Software testing methods and techniques - overview of basic knowledge
- [getting started] intercepting strings
- matlab保存DB4i深度相机图片
- Li Kou daily question - day 31 -1502 Judge whether an arithmetic sequence can be formed
- Tupu software has passed CMMI5 certification| High authority and high-level certification in the international software field
- Eigen matrix operation Library
- 【入门】输入n个整数,输出其中最小的k个
- 0 basic introduction to single chip microcomputer: how to use digital multimeter and precautions
- web254
猜你喜欢

Eigen matrix operation Library

window c盘满了

PWN attack and defense world int_ overflow
![[getting started] intercepting strings](/img/16/363baa4982408f55493057200bcba5.png)
[getting started] intercepting strings

Microsoft stream - how to modify video subtitles

2022 test questions and mock examinations for main principals of hazardous chemicals business units

Gui Gui programming (XV) - use scale to control font size changes

Vhost kick & call principle

Minecraft 1.16.5 module development (51) tile entity

How to make the two financial transactions faster
随机推荐
Significance and measures of source code encryption
Kickback -- find the first palindrome character in a group of characters
AArdio - 【问题】bass库回调时内存增长的问题
Android screen adaptation (using constraintlayout), kotlin array sorting
How relational databases work
Instead of houses, another kind of capital in China is rising
Contenttype comparison of all types
golang中的正则表达式使用注意事项与技巧
matlab保存DB4i深度相机图片
2022 Guangdong Provincial Safety Officer a certificate third batch (main person in charge) special operation certificate examination question bank simulated examination platform operation
力扣每日一题-第31天-1502.判断能否形成等差数列
【力扣10天SQL入门】Day9 控制流
web254
Thesis learning -- Analysis and Research on similarity query of hydrological time series
TCP/UDP 通信问题整理
Insufficient executors to build thread pool
Sqlalchemy creating MySQL_ Table
【入门】取近似值
Missing API interface actual development series (14): ID card real name authentication verification
Set up file server Minio for quick use