当前位置:网站首页>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);
}
}
边栏推荐
猜你喜欢
【入门】截取字符串
[getting started] enter the integer array and sorting ID, and sort its elements in ascending or descending order
Significance and measures of source code encryption
图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
Differential: definition of total differential, partial derivative, gradient
[getting started] input n integers and output the smallest K of them
Connect timed out of database connection
Latex table
Caesar
She is the "HR of others" | ones character
随机推荐
How to prevent the other party from saying that he has no money after winning the lawsuit?
软键盘高度报错
一套十万级TPS的IM综合消息系统的架构实践与思考
go通用动态重试机制解决方案的实现与封装
Access报表实现小计功能
[untitled]
String coordinates of number to excel
EDA开源仿真工具verilator入门6:调试实例
IMDB practice of emotion classification (simplernn, LSTM, Gru)
[getting started] enter the integer array and sorting ID, and sort its elements in ascending or descending order
【力扣10天SQL入门】Day9 控制流
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
Scala language learning-07-constructor
How to make the two financial transactions faster
【无标题】
【力扣10天SQL入门】Day10 控制流
Microsoft stream - how to modify video subtitles
How to get a SharePoint online site created using the office365 group template
The Windows C disk is full