Android: two instances of Text-to-Speech work very slowly

I need to implement a function in my Andorind application that allows you to play two different synthesized languages ​​in the current Acitivity - for example, with two Say English and Say French buttons

I tried to do this in two ways, but both of them work inefficiently, because there is a long delay before playing the sound:

  • first approach: create a single TTS instance and change the language using the setLocale method, depending on which language you want to play. Unfortunately, switching between languages ​​on setLocale is time consuming, which affects the response after clicking a button
  • second approach: create two TTS instances for the corresponding language. Unfortunately, there is also a delay here, and there is no difference between the first solution.

Could you help solve this unpleasant problem?

+6
source share
3 answers

How about two TTS mechanisms complete initialization at the beginning of your application before any user interaction (do this by creating an OnInitListener and waiting - for example, using a semaphore - until the onInit () method is called), so that the moment the user reaches the point in the application where the buttons are entered, you have two already initialized engines

0
source

I solved this problem by creating these instances together in a thread other than the main application thread:

private class tempTask extends AsyncTask { ... @Override protected Object doInBackground(Object... params) { firstTTSObj = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() { @Override public void onInit(int status) { if(status != TextToSpeech.ERROR){ firstTTSObj.setLanguage(Locale.UK); } } }); secondTTSObj = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() { @Override public void onInit(int status) { if(status != TextToSpeech.ERROR){ secondTTSObj .setLanguage(Locale.KOREA); } } }); return null; } } 

Hope this helps.

0
source

It seems that is exactly how TextToSpeech is designed. Guess that it can only load one language value at a time, and when you request another, it should download the current language data and load a new one. I added a problem here: https://code.google.com/p/android/issues/detail?id=200974

Google will probably fix this in the future. They did it!

As a workaround, now you can pre-synthesize the audio file and then just play it. FG, you can run some AsyncTask when starting Activity and generate sound in the background. And when it is ready, open the button in the user interface so that the user can play it. I also found that TextToSpeech is better to use its own service using the android: process attribute for the service, to avoid blocking the user interface on some devices when setLanguage loads language data.

UPDATE 2016-02-17: I just updated TextToSpeech to version 3.8.14 from February 15th. And no more delays! I use 2 languages ​​that read different language texts one by one and do not linger between them! Thanks to the Google team and TextToSpeech! You are the best!

UPDATE 2016-02-18: I tested more devices and found that the problem still exists for older devices where Android 4.x is still in use. Tested on XOOM with Android 4.1.2 and some unavailable device 7100 with Android 4.1.1. Both still have the same delays. Not sure if this is related, but I noticed that both Android devices with Android 4.x have a problem with UtteranceProgressListener. To make it work on such devices, I had to pass the utteranceId parameter to the talk () function. However, the device in which this error disappeared does an excellent job with this parameter. The device where it works fine now is LG d405 with Android 5.0.2. And it had the same latency problem before I installed the latest TextToSpeech update.

0
source

Source: https://habr.com/ru/post/978327/


All Articles