foreword
In Android, the full name of TTS is Text to Speech. You can literally understand what the problem it solves is to convert text into voice service, which means that you input a piece of text information, and then the Android system can broadcast the text. This kind of application scenario is currently mostly on various voice assistant APPs. Many mobile phone system integrators have built-in text-to-speech services, which can read the text information on the current page. Similarly, we can also see related services on some reading apps. When you open WeChat to read, you can directly play the current page in voice mode, which is especially suitable for scenarios where it is inconvenient to read with a mobile phone screen.
The Android system has supported TTS since version 1.6, but unfortunately the default TTS engine of the system: Pico TTS does not support Chinese.
After searching a lot of relevant information, I found that many resources have expired, the APK cannot be found, and the corresponding page is 404. More manufacturers have switched to open platforms and even no longer provide offline/free services.
Offline TTS found so far
Note: The links below may require access to the Internet before they can be accessed
Application Name | support offline | Remark | download link |
---|---|---|---|
ITRI TTS | yes | none | download |
Xunfei Yuji | yes | You need to open it once, but you don't need to log in | download |
HKUST Xunfei Speech Engine 3.0 | yes | recommend | download |
Speech Services by Google | no | You need to surf the Internet scientifically, and you need to download the voice pack first when offline | download |
use
First, install the corresponding TTS application, and then set the corresponding voice output service in the settings. The
general setting steps are: Settings > Accessibility > Text-to-speech (TTS) output.
The program call only needs to use the standard TTS interface of the SDK
. In the article introducing the use of TTS, the code in the following paragraph has no effect on the previous HKUST Xunfei or ITRI TTS, it just adds a pop-up window. Of course, it can also be understood that these two apps are not developed according to the standard interface .
//显示应用选择窗.
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
private TextToSpeech mTts;
protected void onActivityResult(
int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
// success, create the TTS instance
mTts = new TextToSpeech(this, this);
} else {
// missing data, install it
Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}
Print the languages and voices supported by TTS getAvailableLanguages and getVoices in the program :
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
Set<Locale> ava = mTts.getAvailableLanguages();
if (ava != null) {
for (Locale l : ava) {
Logger.v(TAG, "supported " + l.toString());
}
} else {
Logger.d(TAG, "no supported language");
}
Set<Voice> voices = mTts.getVoices();
if(voices != null){
for(Voice v : voices){
Logger.d(TAG, "TTS Voice: " + v.toString());
}
}
}
String engine = mTts.getDefaultEngine();
Logger.d(TAG, "TTS Engine = " + engine);
2022-10-18 15:21:22.577 25266-25266 TextToSpeech com.tts.test I Connected to ComponentInfo{tw.pu.tts/tw.pu.tts.UttsService}
2022-10-18 15:21:22.582 25266-26006 TextToSpeech com.tts.test I Set up connection to ComponentInfo{tw.pu.tts/tw.pu.tts.UttsService}
2022-10-18 15:21:22.594 25266-25266 TTS com.tts.test D onInit 0
2022-10-18 15:21:22.650 25266-25266 TTS com.tts.test V supported zh
2022-10-18 15:21:22.650 25266-25266 TTS com.tts.test V supported en
2022-10-18 15:21:22.650 25266-25266 TTS com.tts.test V supported zh__#Hans
2022-10-18 15:21:22.650 25266-25266 TTS com.tts.test V supported zh__#Hant
2022-10-18 15:21:22.690 25266-25266 TTS com.tts.test D TTS Voice: Voice[Name: zh, locale: zh__#Hans, quality: 300, latency: 300, requiresNetwork: false, features: []]
2022-10-18 15:21:22.691 25266-25266 TTS com.tts.test D TTS Voice: Voice[Name: en, locale: en, quality: 300, latency: 300, requiresNetwork: false, features: []]
2022-10-18 15:21:22.691 25266-25266 TTS com.tts.test D TTS Voice: Voice[Name: zh, locale: zh, quality: 300, latency: 300, requiresNetwork: false, features: []]
2022-10-18 15:21:22.691 25266-25266 TTS com.tts.test D TTS Voice: Voice[Name: zh, locale: zh__#Hant, quality: 300, latency: 300, requiresNetwork: false, features: []]
2022-10-18 15:21:22.698 25266-25266 TTS com.tts.test D TTS Engine = tw.pu.tts
Note: The function of TTS needs to be called back in TextToSpeech.OnInitListener , and it can start to work after confirming that the status is normal
Complete codeTTS.java
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.speech.tts.TextToSpeech;
import android.speech.tts.Voice;
import java.util.Locale;
import java.util.Set;
public class TTS implements TextToSpeech.OnInitListener {
final String TAG = "TTS";
TextToSpeech mTts;
public TTS(Context context){
mTts = new TextToSpeech(context, this);
}
public TTS(Context context, TextToSpeech.OnInitListener lis){
mTts = new TextToSpeech(context, this);
setOnInitListener(lis);
}
final int DATA_CHECK_CODE = 0x774;
@Deprecated
public TTS(Activity activity){
Intent checkIntent = new Intent(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
activity.startActivityForResult(checkIntent, 0x774);
}
@Deprecated
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
if (requestCode == DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
// success, create the TTS instance
mTts = new TextToSpeech(activity, this);
} else {
// missing data, install it
Intent installIntent = new Intent(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
activity.startActivity(installIntent);
}
}
}
public TextToSpeech getTts(){
return mTts;}
private void dump(){
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
Set<Locale> ava = mTts.getAvailableLanguages();
if (ava != null) {
for (Locale l : ava) {
Logger.v(TAG, "supported " + l.toString());
}
//mTts.setLanguage(Locale.US);
} else {
Logger.d(TAG, "no supported language");
}
Set<Voice> voices = mTts.getVoices();
if(voices != null){
for(Voice v : voices){
Logger.d(TAG, "TTS Voice: " + v.toString());
}
}
}
String engine = mTts.getDefaultEngine();
Logger.d(TAG, "TTS Engine = " + engine);
}
public void setSpeechSpeed(float speed){
mTts.setSpeechRate(speed);
}
public void setVoiceFreq(float freq){
mTts.setPitch(freq);
}
public void speak(String s){
if(mTts == null){
Logger.e(TAG, "speak failed: TTS not ready");
return;
}
int res = mTts.speak(s, TextToSpeech.QUEUE_ADD, null);
if(res != 0){
Logger.w(TAG, "speak failed: " + res);
}else{
Logger.d(TAG, "speak " + s + " done");
}
}
public void stop(){
mTts.stop();
}
@Override
public void onInit(int status) {
Logger.d(TAG, "onInit " + status);
if(status == 0) {
dump();
mTts.setPitch(1f);
mTts.setSpeechRate(1f);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
AudioAttributes attr = new AudioAttributes.Builder().setLegacyStreamType(AudioManager.STREAM_MUSIC).build();
mTts.setAudioAttributes(attr);
}
}
if(initLis != null)initLis.onInit(status);
}
TextToSpeech.OnInitListener initLis;
public void setOnInitListener(TextToSpeech.OnInitListener initLis){
this.initLis = initLis;
}
}
reference
Android Chinese Speech Engine Resources
Android uses its own TextToSpeech to realize offline speech synthesis function
The 7 Best Text-to-Speech Apps for Android
The free text-to-speech function provided in Android The quick start usage of TextToSpeech (Android TTS speech synthesis broadcast)
An introduction to Text-To-Speech in Android
Attachment:
Text-to-speech (TTS) application (APK)