brief introduction
TTS is text to speech
1, Use the SDK or API provided by a third party, such as iFLYTEK, Baidu, Alibaba, etc;
2, Use the API of Android system: texttospeech
API documentation in TextToSpeech (official): TextToSpeech | Android Developers , record the detailed introduction of each method, variable and constant;
3, Use of TextToSpeech
The following code is a simple package;
public class TTSUtils extends UtteranceProgressListener { private Context mContext; private static TTSUtils singleton; private TextToSpeech textToSpeech; // System voice broadcast class private boolean isSuccess = true; public static TTSUtils getInstance(Context context) { if (singleton == null) { synchronized (TTSUtils.class) { if (singleton == null) { singleton = new TTSUtils(context); } } } return singleton; } private TTSUtils(Context context) { this.mContext = context.getApplicationContext(); textToSpeech = new TextToSpeech(mContext, i -> { //System voice initialization succeeded if (i == TextToSpeech.SUCCESS) { int result = textToSpeech.setLanguage(Locale.CHINA); textToSpeech.setPitch(2.0f);// Set the tone. The higher the value, the sharper the sound (girls). The lower the value, it will become a male voice. 1.0 is normal textToSpeech.setSpeechRate(1.0f); textToSpeech.setOnUtteranceProgressListener(TTSUtils.this); if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) { //The system does not support Chinese broadcasting isSuccess = false; } } });//Baidu's playback engine "com.baidu.duersdk.opensdk" } public void playText(String playText) { if (!isSuccess) { Toast.makeText(mContext, "The system does not support Chinese broadcasting", Toast.LENGTH_SHORT).show(); return; } if (textToSpeech != null) { textToSpeech.speak(playText, TextToSpeech.QUEUE_ADD, null, null); } } public void stopSpeak() { if (textToSpeech != null) { textToSpeech.stop(); textToSpeech.shutdown(); } } @Override public void onStart(String utteranceId) { } @Override public void onDone(String utteranceId) { } @Override public void onError(String utteranceId) { } }
use
Initialize in the Application or in the Activity first. Because TextToSpeech initialization takes time, initialize early before use, otherwise there will be a pit;
TTSUtils.getInstance(this);
call
TTSUtils.getInstance(getContext()).playText("bright moon in front of bed, suspected frost on the ground");
problem
No sound or prompt. Chinese is not supported
1. Check whether the system is equipped with a playback engine. Take glory V30 as an example to set - > auxiliary functions - > accessibility - > whether a playback engine is installed in text to voice; The system defaults to Pico TTS, but this does not support Chinese;
For the default engine, you can also use the in TextToSpeech
getDefaultEngine()
Method acquisition;
A third-party playback engine needs to be installed, such as com.google.android.tts Google text to voice engine, which does not support systems below 5.0; com.iflytek.speechcloud iFLYTEK voice engine 3.0, supporting systems above 4.0; com.baidu.duersdk.opensdk secret voice engine 3 Systems below 5.0 are not supported;
Baidu network disk download address Password: 78td
2. Install the engine according to your needs
3. Set the engine in the code
When creating TextToSpeech, there is a construction method to specify the engine;
Public texttospeech (context, oninitlistener, listener, string engine) {/ / engine is the engine to be developed. This parameter is actually the package name of the engine. Baidu's playback engine "com.baidu.duersdk.opensdk" this(context, listener, engine, null, true);}
Engine is the engine to be developed. This parameter is actually the package name of the engine. Baidu's playback engine "com.baidu.duersdk.opensdk"
4. If it is not set in the code, you can find "text to voice" in the settings of Android devices (generally under the "accessibility" menu) and modify your own engine to replace the system default Pico TTS;