当前位置:网站首页>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);
}
}
边栏推荐
- Set up file server Minio for quick use
- XX attack - reflective XSS attack hijacking user browser
- PHP laravel wechat payment
- Teach you how to apply for domestic trademark online step by step
- Analysis of slice capacity expansion mechanism
- empirical study and case study
- Access报表实现小计功能
- 【刷题】字符统计【0】
- Aardio - 阴影渐变文字
- [getting started] extract non repeating integers
猜你喜欢

Utiliser Beef pour détourner le navigateur utilisateur

web254

Day5: scanner object, next() and nextline(), sequential structure, selection structure, circular structure

window c盘满了
![[introduction] approximate value](/img/6b/597178d848dd21110f36601fc31092.png)
[introduction] approximate value

軟鍵盤高度報錯

使用 setoolkit 伪造站点窃取用户信息

How to use layui to display the data in the database in the form of tables

IMDB practice of emotion classification (simplernn, LSTM, Gru)
![[staff] high and low octave mark (the notes in the high octave mark | mark range are increased by one octave as a whole | low octave mark | mark range are decreased by one octave as a whole)](/img/ff/ebd936eaa6e57b1eabb691b0642957.jpg)
[staff] high and low octave mark (the notes in the high octave mark | mark range are increased by one octave as a whole | low octave mark | mark range are decreased by one octave as a whole)
随机推荐
Contenttype comparison of all types
Find the nearest n-th power of 2
[getting started] extract non repeating integers
Caesar
Hijacking a user's browser with beef
Access报表实现小计功能
[staff] key number (key number identification position | key number marking list | a major key identification principle | F, C, G position marking ascending | F major key identification principle | B
postgresql源码学习(26)—— Windows vscode远程调试Linux上的postgresql
php laravel微信支付
How to prevent the other party from saying that he has no money after winning the lawsuit?
软键盘高度报错
Aardio - Method of self constructed geticonhandle
初学者如何正确理解google官方建议架构原则(疑问?)
力扣每日一题-第31天-1502.判断能否形成等差数列
PWN attack and defense world int_ overflow
SQL number injection and character injection
Php laraver Wechat payment
window c盘满了
Office365 - how to use stream app to watch offline files at any time
【入门】取近似值