当前位置:网站首页>Anddroid text to speech TTS implementation
Anddroid text to speech TTS implementation
2022-07-01 08:09:00 【Linqingsu】
One 、 Recently, there is a face recognition project. After the recognition is successful, there will be a voice broadcast , But the three parties provided Demo This function is not available in , So understand TTS Speech synthesis technology can realize , To install one in advance TTS The engine then calls native TTS, The source code and engine are attached below apk,
AndroidManifest.xml jurisdiction
<!-- Go to sdcard Permission to write data in -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- Go to sdcard Permission to read data in -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Two 、 The above is a tool class encapsulated here , It is very simple to call directly , The normal speaking speed here may be a little fast , It can be defined according to the situation .
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() {
}
/** * Broadcast * * @param context * @param text Text content */
public static void speak(Context context, String text) {
speak(context, text, 1.0f, 1.0f, false);
}
/** * Broadcast * * @param context * @param text Text content * @param ignoreIfSpeaking If you are broadcasting, ignore */
public static void speak(Context context, String text, boolean ignoreIfSpeaking) {
speak(context, text, 1.0f, 1.0f, ignoreIfSpeaking);
}
/** * Broadcast * * @param context * @param text Text content * @param rate The speed 1.0 It's normal * @param pitch intonation 1.0 It's normal * @param ignoreIfSpeaking If you are broadcasting, ignore */
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");
}
}
}
3、 ... and 、MainActivity Use steps
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 {
/** * Remember to install TTS apk engine Otherwise it won't work Under the project directory kdxf_tts_3.0.apk For the engine apk * @param savedInstanceState */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/** * TextView Text */
TextView textView = findViewById(R.id.text_view);
/** * Broadcast * * @param context * @param text Text content * @param rate The speed 1.0 It's normal * @param pitch intonation 1.0 It's normal * @param ignoreIfSpeaking If you are broadcasting, ignore */
TTSUtils.speak(MainActivity.this,textView.getText().toString(),0.5f,0.5f,true);
}
}
边栏推荐
- Erreur de hauteur du clavier souple
- On June 30, 2022, the record of provincial competition + national competition of Bluebridge
- getInputStream() has already been called for this request
- Rk3399 platform development series explanation (network debugging) 7.30. What will affect the sending process of TCP packets?
- getInputStream() has already been called for this request
- Vhost kick & call principle
- 【网站架构】一招搞定90%的分布式事务,实打实介绍数据库事务、分布式事务的工作原理应用场景
- Serial port oscilloscope software ns-scope
- Connect timed out of database connection
- 数字转excel的字符串坐标
猜你喜欢

Significance and measures of source code encryption

Learn the knowledge you need to know about the communication protocol I2C bus

LSTM of RNN

Latex formula code

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

How to check ad user information?
![[getting started] extract non repeating integers](/img/88/3e96df88e980bd98ac112b18a8678c.png)
[getting started] extract non repeating integers
![[getting started] input n integers and output the smallest K of them](/img/b8/20852484f10bc968d529e9c1ff5480.png)
[getting started] input n integers and output the smallest K of them

Android screen adaptation (using constraintlayout), kotlin array sorting

Why some people earn nearly 10billion a year, while others earn 3000 a month: the details you ignore actually make the most money
随机推荐
数字转excel的字符串坐标
Significance and measures of source code encryption
sqlalchemy创建MySQL_Table
window c盘满了
Precautions and skills in using regular expressions in golang
Set up file server Minio for quick use
How to troubleshoot SharePoint online map network drive failure?
Thesis learning -- Analysis and Research on similarity query of hydrological time series
web254
go通用动态重试机制解决方案的实现与封装
[getting started] input n integers and output the smallest K of them
[website architecture] solve 90% of distributed transactions in one move, and introduce the working principles and application scenarios of database transactions and distributed transactions
Aardio - 自己构造的getIconHandle的方法
XX攻击——反射型 XSS 攻击劫持用户浏览器
SharePoint - how to quickly check whether SharePoint is standard or enterprise edition?
Hijacking a user's browser with beef
[getting started] enter the integer array and sorting ID, and sort its elements in ascending or descending order
5大组合拳,解决校园6大难题,护航教育信息化建设
Airsim雷达相机融合生成彩色点云
QT -- 1. QT connection database