当前位置:网站首页>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);
}
}
边栏推荐
- Kickback -- find the first palindrome character in a group of characters
- window c盘满了
- Scala language learning-07-constructor
- 【Redis】一气呵成,带你了解Redis安装与连接
- Wang Yingqi, founder of ones, talks to fortune (Chinese version): is there any excellent software in China?
- Chinese font Gan: zi2zi
- 三极管是一项伟大的发明
- Sorting out tcp/udp communication problems
- The triode is a great invention
- How to troubleshoot SharePoint online map network drive failure?
猜你喜欢
随机推荐
力扣每日一题-第31天-1790.仅执行一次字符串交换能否使两个字符串相等
[R language] age sex frequency matching select samples for case-control study, and perform frequency matching on age and sex
力扣——求一组字符中的第一个回文字符
PHP laravel wechat payment
SharePoint - modify web application authentication using PowerShell
slice扩容机制分析
Contenttype comparison of all types
【无标题】
Gru of RNN
Source code analysis of open source API gateway APIs IX
php laravel微信支付
getInputStream() has already been called for this request
Uni hot update
[batch DOS CMD summary] extension variables - delay variables CMD /v:on, CMD /v:off, SETLOCAL enabledelayedexpansion, disabledelayedexpansion
Li Kou daily question - day 31 -1790 Can a string exchange be performed only once to make two strings equal
Li Kou daily question - Day 32 -1822 Symbol of array element product
H5 页面设置了字体的粗细样式,但是在华为手机里微信打开访问样式不生效?
php laravel微信支付
postgresql源码学习(26)—— Windows vscode远程调试Linux上的postgresql
三极管是一项伟大的发明