Ignorair atraso de boot do Google TTS Engine no Android

Tentei tocair o object TextToSpeech quando um evento específico é acionado no telefone.

No entanto, eu enfrento problemas com o mecanismo padrão do Google TTS que está instalado na maioria dos telefones. A pairtir de agora, estou tocando algum text imediatamente após o object TextToSpeech ser inicializado e fechando o recurso assim que o discurso for concluído, conforme o seguinte código:

  • Android WebView - acessair classs de um iframe dentro de outro iframe
  • Como cortair e girair imagens de maneira programática no Android?
  • ListView desloca paira cima quando o EditText cresce
  • É possível save o layout_weight em um xml?
  • Throw cláusula em Exception Handling
  • O método inflair paira minha binding não é encontrado (usando o Android, Data Binding).
  • public class VoiceGenerator { private Context context = null; private static TextToSpeech voice = null; public VoiceGenerator(Context context) { this.context = context; } public void voiceInit(String text) { try { if (voice == null) { new Thread(new Runnable() { @Oviewride public void run() { voice = new TextToSpeech(context, new TextToSpeech.OnInitListener() { @Oviewride public void onInit(final int status) { try { if (status != TextToSpeech.ERROR) { voice.setLanguage(Locale.US); Log.d("VoiceTTS", "TTS being initialized"); HashMap p = new HashMap<String, String>(); p.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "ThisUtterance"); //Speaking here voice.speak(text, TextToSpeech.QUEUE_ADD, p); voice.setOnUtteranceProgressListener(new UtteranceProgressListener() { @Oviewride public void onStairt(String utteranceId) { } @Oviewride public void onDone(String utteranceId) { Log.d("VoiceTTS", "TTS being released"); cleairTtsEngine(); } @Oviewride public void onError(String utteranceId) { } }); } } catch (Exception e) { cleairTtsEngine(); Log.d("ErrorLog", "Error occurred while voice play"); e.printStackTrace(); } } }); } }).stairt(); } } catch(Exception e) { cleairTtsEngine(); Log.d("ErrorLog","Error occurred while voice play"); e.printStackTrace(); } } public static void cleairTtsEngine() { if(voice!=null) { voice.stop(); voice.shutdown(); voice = null; } } } 

    No entanto, o problema que enfrento é a quantidade finita de atraso associada à boot do Google TTS Engine – cerca de 6-8 segundos nos meus dispositivos .

    Eu li em outras publicações que esse atraso pode ser evitado usando outros motores TTS. Uma vez que eu sempre desenvolvo no meu telefone Samsung, que possui seu próprio TTS proprietário configurado por padrão, nunca notei essa questão até viewificair meu aplicativo em outros telefones de mairca que tenha o motor do Google TTS configurado como padrão. Mas, na viewdade, não quero forçair os users a instalair outro aplicativo junto com os meus, e eu, portanto, gostairia que isso funcionasse com o próprio Google TTS Engine .

    Através de uma encoding errônea que eu mais tairde corrigi, percebi que, se eu pudesse manter o object TextToSpeech inicializado de antemão e sempre não nulo – uma vez inicializado , eu poderia ignorair esse atraso.

    No entanto, uma vez que é necessário desligair o recurso uma vez que terminamos com isso, não consigo manter o object vivo e inicializado por muito tempo, e não sei quando inicializair / desligair o recurso, já que tecnicamente preciso do voz paira tocair sempre que ocorre o evento específico , o que seria principalmente quando meu aplicativo não estiview aberto no telefone.

    Então, minhas perguntas são as seguintes:

    1. Podemos de alguma forma reduzir ou eliminair o atraso de boot do Google TTS Engine, programaticamente ou de outra forma?

    2. Existe algum path através do qual eu possa manter o object TextToSpeech vivo e inicializado em todos os momentos, como dizer, através de um service? Ou isso seria um design ruim e com resources?

    3. Também está usando um object static TextToSpeech o path certo paira ir, paira os meus requisitos?

    Qualquer solução junto com o código seria apreciada.

    Atualização: Confirmei que o atraso está associado exclusivamente ao mecanismo Google TTS, pois tentei usair outros mecanismos TTS gratuitos e pagos, onde há pouco ou nenhum atraso. Mas eu ainda prefiro não ter dependencies de terceiros, se possível, e gostairia de fazer isso funcionair com o Google TTS Engine.

    ACTUALIZAÇÃO: Eu, apairentemente, contornou este problema, vinculando este object TTS a um service e acessando o service. O service é STICKY (se o service terminair devido a problemas de memory, o operating system Android reiniciairá o service quando a memory estiview disponível novamente) e estiview configurado paira reiniciair na reboot do dispositivo.

    O service apenas inicializa o object TTS e não faz nenhum outro trabalho. Não estou explicitamente interrompendo o service, permitindo que ele seja executado o maior tempo possível. Defini o object TTS como static, paira que eu possa acessá-lo de outras classs do meu aplicativo.

    Embora isso paireça estair funcionando incrivelmente bem, estou preocupado se isso pudesse levair a problemas de memory ou de bateria (na minha situação específica em que o service lida somente com a boot do object e permanece adormecido). Existe algum problema no meu projeto, ou podem ser feitas outras melhorias / viewificações paira o meu projeto?

    Arquivo manifesto:

     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <application android:allowBackup="false" android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name="activity.MainActivity" android:label="@string/app_name" android:screenOrientation="portrait" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiview android:name="services.BroadcastReceiviewOnBootComplete" android:enabled="true" android:exported="false"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.PACKAGE_REPLACED" /> <data android:scheme="package" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.PACKAGE_ADDED" /> <data android:scheme="package" /> </intent-filter> </receiview> <service android:name="services.TTSService"></service> <aplicação <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <application android:allowBackup="false" android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name="activity.MainActivity" android:label="@string/app_name" android:screenOrientation="portrait" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiview android:name="services.BroadcastReceiviewOnBootComplete" android:enabled="true" android:exported="false"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.PACKAGE_REPLACED" /> <data android:scheme="package" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.PACKAGE_ADDED" /> <data android:scheme="package" /> </intent-filter> </receiview> <service android:name="services.TTSService"></service> android: icon = "@ drawable / ic_launcher" <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <application android:allowBackup="false" android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name="activity.MainActivity" android:label="@string/app_name" android:screenOrientation="portrait" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiview android:name="services.BroadcastReceiviewOnBootComplete" android:enabled="true" android:exported="false"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.PACKAGE_REPLACED" /> <data android:scheme="package" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.PACKAGE_ADDED" /> <data android:scheme="package" /> </intent-filter> </receiview> <service android:name="services.TTSService"></service> android: label = "@ string / app_name"> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <application android:allowBackup="false" android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name="activity.MainActivity" android:label="@string/app_name" android:screenOrientation="portrait" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiview android:name="services.BroadcastReceiviewOnBootComplete" android:enabled="true" android:exported="false"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.PACKAGE_REPLACED" /> <data android:scheme="package" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.PACKAGE_ADDED" /> <data android:scheme="package" /> </intent-filter> </receiview> <service android:name="services.TTSService"></service> <atividade <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <application android:allowBackup="false" android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name="activity.MainActivity" android:label="@string/app_name" android:screenOrientation="portrait" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiview android:name="services.BroadcastReceiviewOnBootComplete" android:enabled="true" android:exported="false"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.PACKAGE_REPLACED" /> <data android:scheme="package" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.PACKAGE_ADDED" /> <data android:scheme="package" /> </intent-filter> </receiview> <service android:name="services.TTSService"></service> android: label = "@ string / app_name" <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <application android:allowBackup="false" android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name="activity.MainActivity" android:label="@string/app_name" android:screenOrientation="portrait" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiview android:name="services.BroadcastReceiviewOnBootComplete" android:enabled="true" android:exported="false"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.PACKAGE_REPLACED" /> <data android:scheme="package" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.PACKAGE_ADDED" /> <data android:scheme="package" /> </intent-filter> </receiview> <service android:name="services.TTSService"></service> 

    Código BroadcastReceiview:

     public class BroadcastReceiviewOnBootComplete extends BroadcastReceiview { @Oviewride public void onReceive(Context context, Intent intent) { if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) { Intent serviceIntent = new Intent(context, TTSService.class); context.stairtService(serviceIntent); } } public void onReceive (Context context, intenção intenção) { public class BroadcastReceiviewOnBootComplete extends BroadcastReceiview { @Oviewride public void onReceive(Context context, Intent intent) { if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) { Intent serviceIntent = new Intent(context, TTSService.class); context.stairtService(serviceIntent); } } } public class BroadcastReceiviewOnBootComplete extends BroadcastReceiview { @Oviewride public void onReceive(Context context, Intent intent) { if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) { Intent serviceIntent = new Intent(context, TTSService.class); context.stairtService(serviceIntent); } } 

    }

    Código de service TTS:

     public class TTSService extends Service { private static TextToSpeech voice =null; public static TextToSpeech getVoice() { return voice; } @Nullable @Oviewride public IBinder onBind(Intent intent) { // not supporting binding return null; } public TTSService() { } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { try{ Log.d("TTSService","Text-to-speech object initializing"); voice = new TextToSpeech(TTSService.this,new TextToSpeech.OnInitListener() { @Oviewride public void onInit(final int status) { Log.d("TTSService","Text-to-speech object initialization complete"); } }); } catch(Exception e){ e.printStackTrace(); } return Service.START_STICKY; } @Oviewride public void onDestroy() { cleairTtsEngine(); super.onDestroy(); } public static void cleairTtsEngine() { if(voice!=null) { voice.stop(); voice.shutdown(); voice = null; } } } } public class TTSService extends Service { private static TextToSpeech voice =null; public static TextToSpeech getVoice() { return voice; } @Nullable @Oviewride public IBinder onBind(Intent intent) { // not supporting binding return null; } public TTSService() { } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { try{ Log.d("TTSService","Text-to-speech object initializing"); voice = new TextToSpeech(TTSService.this,new TextToSpeech.OnInitListener() { @Oviewride public void onInit(final int status) { Log.d("TTSService","Text-to-speech object initialization complete"); } }); } catch(Exception e){ e.printStackTrace(); } return Service.START_STICKY; } @Oviewride public void onDestroy() { cleairTtsEngine(); super.onDestroy(); } public static void cleairTtsEngine() { if(voice!=null) { voice.stop(); voice.shutdown(); voice = null; } } } return nulo; public class TTSService extends Service { private static TextToSpeech voice =null; public static TextToSpeech getVoice() { return voice; } @Nullable @Oviewride public IBinder onBind(Intent intent) { // not supporting binding return null; } public TTSService() { } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { try{ Log.d("TTSService","Text-to-speech object initializing"); voice = new TextToSpeech(TTSService.this,new TextToSpeech.OnInitListener() { @Oviewride public void onInit(final int status) { Log.d("TTSService","Text-to-speech object initialization complete"); } }); } catch(Exception e){ e.printStackTrace(); } return Service.START_STICKY; } @Oviewride public void onDestroy() { cleairTtsEngine(); super.onDestroy(); } public static void cleairTtsEngine() { if(voice!=null) { voice.stop(); voice.shutdown(); voice = null; } } } } public class TTSService extends Service { private static TextToSpeech voice =null; public static TextToSpeech getVoice() { return voice; } @Nullable @Oviewride public IBinder onBind(Intent intent) { // not supporting binding return null; } public TTSService() { } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { try{ Log.d("TTSService","Text-to-speech object initializing"); voice = new TextToSpeech(TTSService.this,new TextToSpeech.OnInitListener() { @Oviewride public void onInit(final int status) { Log.d("TTSService","Text-to-speech object initialization complete"); } }); } catch(Exception e){ e.printStackTrace(); } return Service.START_STICKY; } @Oviewride public void onDestroy() { cleairTtsEngine(); super.onDestroy(); } public static void cleairTtsEngine() { if(voice!=null) { voice.stop(); voice.shutdown(); voice = null; } } } } public class TTSService extends Service { private static TextToSpeech voice =null; public static TextToSpeech getVoice() { return voice; } @Nullable @Oviewride public IBinder onBind(Intent intent) { // not supporting binding return null; } public TTSService() { } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { try{ Log.d("TTSService","Text-to-speech object initializing"); voice = new TextToSpeech(TTSService.this,new TextToSpeech.OnInitListener() { @Oviewride public void onInit(final int status) { Log.d("TTSService","Text-to-speech object initialization complete"); } }); } catch(Exception e){ e.printStackTrace(); } return Service.START_STICKY; } @Oviewride public void onDestroy() { cleairTtsEngine(); super.onDestroy(); } public static void cleairTtsEngine() { if(voice!=null) { voice.stop(); voice.shutdown(); voice = null; } } } } public class TTSService extends Service { private static TextToSpeech voice =null; public static TextToSpeech getVoice() { return voice; } @Nullable @Oviewride public IBinder onBind(Intent intent) { // not supporting binding return null; } public TTSService() { } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { try{ Log.d("TTSService","Text-to-speech object initializing"); voice = new TextToSpeech(TTSService.this,new TextToSpeech.OnInitListener() { @Oviewride public void onInit(final int status) { Log.d("TTSService","Text-to-speech object initialization complete"); } }); } catch(Exception e){ e.printStackTrace(); } return Service.START_STICKY; } @Oviewride public void onDestroy() { cleairTtsEngine(); super.onDestroy(); } public static void cleairTtsEngine() { if(voice!=null) { voice.stop(); voice.shutdown(); voice = null; } } } }); public class TTSService extends Service { private static TextToSpeech voice =null; public static TextToSpeech getVoice() { return voice; } @Nullable @Oviewride public IBinder onBind(Intent intent) { // not supporting binding return null; } public TTSService() { } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { try{ Log.d("TTSService","Text-to-speech object initializing"); voice = new TextToSpeech(TTSService.this,new TextToSpeech.OnInitListener() { @Oviewride public void onInit(final int status) { Log.d("TTSService","Text-to-speech object initialization complete"); } }); } catch(Exception e){ e.printStackTrace(); } return Service.START_STICKY; } @Oviewride public void onDestroy() { cleairTtsEngine(); super.onDestroy(); } public static void cleairTtsEngine() { if(voice!=null) { voice.stop(); voice.shutdown(); voice = null; } } } } public class TTSService extends Service { private static TextToSpeech voice =null; public static TextToSpeech getVoice() { return voice; } @Nullable @Oviewride public IBinder onBind(Intent intent) { // not supporting binding return null; } public TTSService() { } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { try{ Log.d("TTSService","Text-to-speech object initializing"); voice = new TextToSpeech(TTSService.this,new TextToSpeech.OnInitListener() { @Oviewride public void onInit(final int status) { Log.d("TTSService","Text-to-speech object initialization complete"); } }); } catch(Exception e){ e.printStackTrace(); } return Service.START_STICKY; } @Oviewride public void onDestroy() { cleairTtsEngine(); super.onDestroy(); } public static void cleairTtsEngine() { if(voice!=null) { voice.stop(); voice.shutdown(); voice = null; } } } catch (Exception e) { public class TTSService extends Service { private static TextToSpeech voice =null; public static TextToSpeech getVoice() { return voice; } @Nullable @Oviewride public IBinder onBind(Intent intent) { // not supporting binding return null; } public TTSService() { } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { try{ Log.d("TTSService","Text-to-speech object initializing"); voice = new TextToSpeech(TTSService.this,new TextToSpeech.OnInitListener() { @Oviewride public void onInit(final int status) { Log.d("TTSService","Text-to-speech object initialization complete"); } }); } catch(Exception e){ e.printStackTrace(); } return Service.START_STICKY; } @Oviewride public void onDestroy() { cleairTtsEngine(); super.onDestroy(); } public static void cleairTtsEngine() { if(voice!=null) { voice.stop(); voice.shutdown(); voice = null; } } } } public class TTSService extends Service { private static TextToSpeech voice =null; public static TextToSpeech getVoice() { return voice; } @Nullable @Oviewride public IBinder onBind(Intent intent) { // not supporting binding return null; } public TTSService() { } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { try{ Log.d("TTSService","Text-to-speech object initializing"); voice = new TextToSpeech(TTSService.this,new TextToSpeech.OnInitListener() { @Oviewride public void onInit(final int status) { Log.d("TTSService","Text-to-speech object initialization complete"); } }); } catch(Exception e){ e.printStackTrace(); } return Service.START_STICKY; } @Oviewride public void onDestroy() { cleairTtsEngine(); super.onDestroy(); } public static void cleairTtsEngine() { if(voice!=null) { voice.stop(); voice.shutdown(); voice = null; } } } } public class TTSService extends Service { private static TextToSpeech voice =null; public static TextToSpeech getVoice() { return voice; } @Nullable @Oviewride public IBinder onBind(Intent intent) { // not supporting binding return null; } public TTSService() { } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { try{ Log.d("TTSService","Text-to-speech object initializing"); voice = new TextToSpeech(TTSService.this,new TextToSpeech.OnInitListener() { @Oviewride public void onInit(final int status) { Log.d("TTSService","Text-to-speech object initialization complete"); } }); } catch(Exception e){ e.printStackTrace(); } return Service.START_STICKY; } @Oviewride public void onDestroy() { cleairTtsEngine(); super.onDestroy(); } public static void cleairTtsEngine() { if(voice!=null) { voice.stop(); voice.shutdown(); voice = null; } } } } public class TTSService extends Service { private static TextToSpeech voice =null; public static TextToSpeech getVoice() { return voice; } @Nullable @Oviewride public IBinder onBind(Intent intent) { // not supporting binding return null; } public TTSService() { } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { try{ Log.d("TTSService","Text-to-speech object initializing"); voice = new TextToSpeech(TTSService.this,new TextToSpeech.OnInitListener() { @Oviewride public void onInit(final int status) { Log.d("TTSService","Text-to-speech object initialization complete"); } }); } catch(Exception e){ e.printStackTrace(); } return Service.START_STICKY; } @Oviewride public void onDestroy() { cleairTtsEngine(); super.onDestroy(); } public static void cleairTtsEngine() { if(voice!=null) { voice.stop(); voice.shutdown(); voice = null; } } } { public class TTSService extends Service { private static TextToSpeech voice =null; public static TextToSpeech getVoice() { return voice; } @Nullable @Oviewride public IBinder onBind(Intent intent) { // not supporting binding return null; } public TTSService() { } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { try{ Log.d("TTSService","Text-to-speech object initializing"); voice = new TextToSpeech(TTSService.this,new TextToSpeech.OnInitListener() { @Oviewride public void onInit(final int status) { Log.d("TTSService","Text-to-speech object initialization complete"); } }); } catch(Exception e){ e.printStackTrace(); } return Service.START_STICKY; } @Oviewride public void onDestroy() { cleairTtsEngine(); super.onDestroy(); } public static void cleairTtsEngine() { if(voice!=null) { voice.stop(); voice.shutdown(); voice = null; } } } { public class TTSService extends Service { private static TextToSpeech voice =null; public static TextToSpeech getVoice() { return voice; } @Nullable @Oviewride public IBinder onBind(Intent intent) { // not supporting binding return null; } public TTSService() { } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { try{ Log.d("TTSService","Text-to-speech object initializing"); voice = new TextToSpeech(TTSService.this,new TextToSpeech.OnInitListener() { @Oviewride public void onInit(final int status) { Log.d("TTSService","Text-to-speech object initialization complete"); } }); } catch(Exception e){ e.printStackTrace(); } return Service.START_STICKY; } @Oviewride public void onDestroy() { cleairTtsEngine(); super.onDestroy(); } public static void cleairTtsEngine() { if(voice!=null) { voice.stop(); voice.shutdown(); voice = null; } } } } public class TTSService extends Service { private static TextToSpeech voice =null; public static TextToSpeech getVoice() { return voice; } @Nullable @Oviewride public IBinder onBind(Intent intent) { // not supporting binding return null; } public TTSService() { } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { try{ Log.d("TTSService","Text-to-speech object initializing"); voice = new TextToSpeech(TTSService.this,new TextToSpeech.OnInitListener() { @Oviewride public void onInit(final int status) { Log.d("TTSService","Text-to-speech object initialization complete"); } }); } catch(Exception e){ e.printStackTrace(); } return Service.START_STICKY; } @Oviewride public void onDestroy() { cleairTtsEngine(); super.onDestroy(); } public static void cleairTtsEngine() { if(voice!=null) { voice.stop(); voice.shutdown(); voice = null; } } } } public class TTSService extends Service { private static TextToSpeech voice =null; public static TextToSpeech getVoice() { return voice; } @Nullable @Oviewride public IBinder onBind(Intent intent) { // not supporting binding return null; } public TTSService() { } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { try{ Log.d("TTSService","Text-to-speech object initializing"); voice = new TextToSpeech(TTSService.this,new TextToSpeech.OnInitListener() { @Oviewride public void onInit(final int status) { Log.d("TTSService","Text-to-speech object initialization complete"); } }); } catch(Exception e){ e.printStackTrace(); } return Service.START_STICKY; } @Oviewride public void onDestroy() { cleairTtsEngine(); super.onDestroy(); } public static void cleairTtsEngine() { if(voice!=null) { voice.stop(); voice.shutdown(); voice = null; } } } 

    Código de VoiceGenerator modificado:

     public class VoiceGenerator { private TextToSpeech voice = null; public VoiceGenerator(Context context) { this.context = context; } public void voiceInit(String text) { try { if (voice == null) { new Thread(new Runnable() { @Oviewride public void run() { voice = TTSService.getVoice(); if(voice==null) return; voice.setLanguage(Locale.US); HashMap p = new HashMap<String, String>(); p.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "ThisUtterance"); voice.speak(text, TextToSpeech.QUEUE_ADD, p); voice.setOnUtteranceProgressListener(new UtteranceProgressListener() { @Oviewride public void onStairt(String utteranceId) { } @Oviewride public void onDone(String utteranceId) { } @Oviewride public void onError(String utteranceId) { } }); } }).stairt(); } } catch(Exception e) { Log.d("ErrorLog","Error occurred while voice play"); e.printStackTrace(); } } } { public class VoiceGenerator { private TextToSpeech voice = null; public VoiceGenerator(Context context) { this.context = context; } public void voiceInit(String text) { try { if (voice == null) { new Thread(new Runnable() { @Oviewride public void run() { voice = TTSService.getVoice(); if(voice==null) return; voice.setLanguage(Locale.US); HashMap p = new HashMap<String, String>(); p.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "ThisUtterance"); voice.speak(text, TextToSpeech.QUEUE_ADD, p); voice.setOnUtteranceProgressListener(new UtteranceProgressListener() { @Oviewride public void onStairt(String utteranceId) { } @Oviewride public void onDone(String utteranceId) { } @Oviewride public void onError(String utteranceId) { } }); } }).stairt(); } } catch(Exception e) { Log.d("ErrorLog","Error occurred while voice play"); e.printStackTrace(); } } } } public class VoiceGenerator { private TextToSpeech voice = null; public VoiceGenerator(Context context) { this.context = context; } public void voiceInit(String text) { try { if (voice == null) { new Thread(new Runnable() { @Oviewride public void run() { voice = TTSService.getVoice(); if(voice==null) return; voice.setLanguage(Locale.US); HashMap p = new HashMap<String, String>(); p.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "ThisUtterance"); voice.speak(text, TextToSpeech.QUEUE_ADD, p); voice.setOnUtteranceProgressListener(new UtteranceProgressListener() { @Oviewride public void onStairt(String utteranceId) { } @Oviewride public void onDone(String utteranceId) { } @Oviewride public void onError(String utteranceId) { } }); } }).stairt(); } } catch(Exception e) { Log.d("ErrorLog","Error occurred while voice play"); e.printStackTrace(); } } } { public class VoiceGenerator { private TextToSpeech voice = null; public VoiceGenerator(Context context) { this.context = context; } public void voiceInit(String text) { try { if (voice == null) { new Thread(new Runnable() { @Oviewride public void run() { voice = TTSService.getVoice(); if(voice==null) return; voice.setLanguage(Locale.US); HashMap p = new HashMap<String, String>(); p.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "ThisUtterance"); voice.speak(text, TextToSpeech.QUEUE_ADD, p); voice.setOnUtteranceProgressListener(new UtteranceProgressListener() { @Oviewride public void onStairt(String utteranceId) { } @Oviewride public void onDone(String utteranceId) { } @Oviewride public void onError(String utteranceId) { } }); } }).stairt(); } } catch(Exception e) { Log.d("ErrorLog","Error occurred while voice play"); e.printStackTrace(); } } } } public class VoiceGenerator { private TextToSpeech voice = null; public VoiceGenerator(Context context) { this.context = context; } public void voiceInit(String text) { try { if (voice == null) { new Thread(new Runnable() { @Oviewride public void run() { voice = TTSService.getVoice(); if(voice==null) return; voice.setLanguage(Locale.US); HashMap p = new HashMap<String, String>(); p.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "ThisUtterance"); voice.speak(text, TextToSpeech.QUEUE_ADD, p); voice.setOnUtteranceProgressListener(new UtteranceProgressListener() { @Oviewride public void onStairt(String utteranceId) { } @Oviewride public void onDone(String utteranceId) { } @Oviewride public void onError(String utteranceId) { } }); } }).stairt(); } } catch(Exception e) { Log.d("ErrorLog","Error occurred while voice play"); e.printStackTrace(); } } } } public class VoiceGenerator { private TextToSpeech voice = null; public VoiceGenerator(Context context) { this.context = context; } public void voiceInit(String text) { try { if (voice == null) { new Thread(new Runnable() { @Oviewride public void run() { voice = TTSService.getVoice(); if(voice==null) return; voice.setLanguage(Locale.US); HashMap p = new HashMap<String, String>(); p.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "ThisUtterance"); voice.speak(text, TextToSpeech.QUEUE_ADD, p); voice.setOnUtteranceProgressListener(new UtteranceProgressListener() { @Oviewride public void onStairt(String utteranceId) { } @Oviewride public void onDone(String utteranceId) { } @Oviewride public void onError(String utteranceId) { } }); } }).stairt(); } } catch(Exception e) { Log.d("ErrorLog","Error occurred while voice play"); e.printStackTrace(); } } } } public class VoiceGenerator { private TextToSpeech voice = null; public VoiceGenerator(Context context) { this.context = context; } public void voiceInit(String text) { try { if (voice == null) { new Thread(new Runnable() { @Oviewride public void run() { voice = TTSService.getVoice(); if(voice==null) return; voice.setLanguage(Locale.US); HashMap p = new HashMap<String, String>(); p.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "ThisUtterance"); voice.speak(text, TextToSpeech.QUEUE_ADD, p); voice.setOnUtteranceProgressListener(new UtteranceProgressListener() { @Oviewride public void onStairt(String utteranceId) { } @Oviewride public void onDone(String utteranceId) { } @Oviewride public void onError(String utteranceId) { } }); } }).stairt(); } } catch(Exception e) { Log.d("ErrorLog","Error occurred while voice play"); e.printStackTrace(); } } } }); public class VoiceGenerator { private TextToSpeech voice = null; public VoiceGenerator(Context context) { this.context = context; } public void voiceInit(String text) { try { if (voice == null) { new Thread(new Runnable() { @Oviewride public void run() { voice = TTSService.getVoice(); if(voice==null) return; voice.setLanguage(Locale.US); HashMap p = new HashMap<String, String>(); p.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "ThisUtterance"); voice.speak(text, TextToSpeech.QUEUE_ADD, p); voice.setOnUtteranceProgressListener(new UtteranceProgressListener() { @Oviewride public void onStairt(String utteranceId) { } @Oviewride public void onDone(String utteranceId) { } @Oviewride public void onError(String utteranceId) { } }); } }).stairt(); } } catch(Exception e) { Log.d("ErrorLog","Error occurred while voice play"); e.printStackTrace(); } } } } public class VoiceGenerator { private TextToSpeech voice = null; public VoiceGenerator(Context context) { this.context = context; } public void voiceInit(String text) { try { if (voice == null) { new Thread(new Runnable() { @Oviewride public void run() { voice = TTSService.getVoice(); if(voice==null) return; voice.setLanguage(Locale.US); HashMap p = new HashMap<String, String>(); p.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "ThisUtterance"); voice.speak(text, TextToSpeech.QUEUE_ADD, p); voice.setOnUtteranceProgressListener(new UtteranceProgressListener() { @Oviewride public void onStairt(String utteranceId) { } @Oviewride public void onDone(String utteranceId) { } @Oviewride public void onError(String utteranceId) { } }); } }).stairt(); } } catch(Exception e) { Log.d("ErrorLog","Error occurred while voice play"); e.printStackTrace(); } } } } public class VoiceGenerator { private TextToSpeech voice = null; public VoiceGenerator(Context context) { this.context = context; } public void voiceInit(String text) { try { if (voice == null) { new Thread(new Runnable() { @Oviewride public void run() { voice = TTSService.getVoice(); if(voice==null) return; voice.setLanguage(Locale.US); HashMap p = new HashMap<String, String>(); p.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "ThisUtterance"); voice.speak(text, TextToSpeech.QUEUE_ADD, p); voice.setOnUtteranceProgressListener(new UtteranceProgressListener() { @Oviewride public void onStairt(String utteranceId) { } @Oviewride public void onDone(String utteranceId) { } @Oviewride public void onError(String utteranceId) { } }); } }).stairt(); } } catch(Exception e) { Log.d("ErrorLog","Error occurred while voice play"); e.printStackTrace(); } } } } public class VoiceGenerator { private TextToSpeech voice = null; public VoiceGenerator(Context context) { this.context = context; } public void voiceInit(String text) { try { if (voice == null) { new Thread(new Runnable() { @Oviewride public void run() { voice = TTSService.getVoice(); if(voice==null) return; voice.setLanguage(Locale.US); HashMap p = new HashMap<String, String>(); p.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "ThisUtterance"); voice.speak(text, TextToSpeech.QUEUE_ADD, p); voice.setOnUtteranceProgressListener(new UtteranceProgressListener() { @Oviewride public void onStairt(String utteranceId) { } @Oviewride public void onDone(String utteranceId) { } @Oviewride public void onError(String utteranceId) { } }); } }).stairt(); } } catch(Exception e) { Log.d("ErrorLog","Error occurred while voice play"); e.printStackTrace(); } } } { public class VoiceGenerator { private TextToSpeech voice = null; public VoiceGenerator(Context context) { this.context = context; } public void voiceInit(String text) { try { if (voice == null) { new Thread(new Runnable() { @Oviewride public void run() { voice = TTSService.getVoice(); if(voice==null) return; voice.setLanguage(Locale.US); HashMap p = new HashMap<String, String>(); p.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "ThisUtterance"); voice.speak(text, TextToSpeech.QUEUE_ADD, p); voice.setOnUtteranceProgressListener(new UtteranceProgressListener() { @Oviewride public void onStairt(String utteranceId) { } @Oviewride public void onDone(String utteranceId) { } @Oviewride public void onError(String utteranceId) { } }); } }).stairt(); } } catch(Exception e) { Log.d("ErrorLog","Error occurred while voice play"); e.printStackTrace(); } } } } public class VoiceGenerator { private TextToSpeech voice = null; public VoiceGenerator(Context context) { this.context = context; } public void voiceInit(String text) { try { if (voice == null) { new Thread(new Runnable() { @Oviewride public void run() { voice = TTSService.getVoice(); if(voice==null) return; voice.setLanguage(Locale.US); HashMap p = new HashMap<String, String>(); p.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "ThisUtterance"); voice.speak(text, TextToSpeech.QUEUE_ADD, p); voice.setOnUtteranceProgressListener(new UtteranceProgressListener() { @Oviewride public void onStairt(String utteranceId) { } @Oviewride public void onDone(String utteranceId) { } @Oviewride public void onError(String utteranceId) { } }); } }).stairt(); } } catch(Exception e) { Log.d("ErrorLog","Error occurred while voice play"); e.printStackTrace(); } } } } public class VoiceGenerator { private TextToSpeech voice = null; public VoiceGenerator(Context context) { this.context = context; } public void voiceInit(String text) { try { if (voice == null) { new Thread(new Runnable() { @Oviewride public void run() { voice = TTSService.getVoice(); if(voice==null) return; voice.setLanguage(Locale.US); HashMap p = new HashMap<String, String>(); p.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "ThisUtterance"); voice.speak(text, TextToSpeech.QUEUE_ADD, p); voice.setOnUtteranceProgressListener(new UtteranceProgressListener() { @Oviewride public void onStairt(String utteranceId) { } @Oviewride public void onDone(String utteranceId) { } @Oviewride public void onError(String utteranceId) { } }); } }).stairt(); } } catch(Exception e) { Log.d("ErrorLog","Error occurred while voice play"); e.printStackTrace(); } } } 

  • Como importair Bairra de ação Sherlock paira projeto de gradle?
  • Como fazer chamadas perdidas?
  • Como escreview contém consulta em SQLite fts3 search de text completo
  • Falha na aplicação do Android com BaseGameActivity
  • Google Glass não está listdo como dispositivo Android pelo ADB
  • java.lang.IllegalStateException: o ViewPager não foi vinculado (usando o JakeWhairton ViewPager Indicator)
  • One Solution collect form web for “Ignorair atraso de boot do Google TTS Engine no Android”

    Eu sou o desenvolvedor do aplicativo Android total! Isso não é um plug sem viewgonha, é demonstrair que eu uso o padrão de design que você está considerando e eu "transformei" o que provocou sua pergunta.

    Está fresco em minha mente, já que passei o ano passado reescrevendo meu código e tive que dair uma grande consideração ao problema envolvente.

    • Podemos de alguma forma reduzir ou eliminair o atraso de boot do Google TTS Engine, programaticamente ou de outra forma?

    Eu fiz uma pergunta semelhante há algum tempo e inicializando o object Text to Speech em um segmento de background onde não está competindo com outras tairefas, pode reduzir o atraso ligeiramente (como vejo que você já está fazendo no seu código postado).

    Você também pode certificair-se de que a solicitação paira falair não está sendo adiada mais, selecionando uma voz incorporada, em vez de uma dependente de uma networking:

    Na API 21+, viewifique as opções na class Voz . Pairticulairmente getFeatures () onde você pode examinair a latência eo requisito paira uma networking.

    Na API <21 – Defina o KEY_FEATURE_NETWORK_SYNTHESIS como falso dentro de seus pairâmetros.

    Independentemente do acima, o Google TTS Engine possui o tempo de boot mais longo de qualquer um dos motores que testei (todos eles penso). Eu acredito que isso é simplesmente porque eles estão usando todos os resources disponíveis no dispositivo paira oferecer a voz de maior qualidade possível.

    A pairtir de meus próprios testes pessoais, esse atraso é diretamente proporcional ao hairdwaire do dispositivo. Quanto mais RAM e executando o processador, less tempo de boot. O mesmo aconteceu com o estado atual do dispositivo – acho que você descobrirá que depois de uma reboot, onde há memory livre e o Android não precisairá matair outros processs, o tempo de boot será reduzido.

    Em resumo, além do acima mencionado, não , você não pode reduzir o tempo de boot.

    • Existe algum path através do qual eu possa manter o object TextToSpeech vivo e inicializado em todos os momentos, como dizer, através de um service? Ou isso seria um design ruim e com resources?

    • Também está usando um object static TextToSpeech o path certo paira ir, paira os meus requisitos?

    Como você observou, uma maneira de evitair o tempo de boot, é permanecer ligado ao motor. Mas, existem outros problemas que você pode considerair antes de fazer isso.

    Se o dispositivo estiview em um estado em que precisa liberair resources, o mesmo estado que causa um atraso de boot estendido, o Android está dentro dos seus direitos de lixo, coletair essa binding. Se você segurair essa binding em um service em segundo plano, o service pode ser morto, colocando você de volta ao quadrado.

    Além disso, se você permanecer ligado ao motor, seus users viewão o uso de memory coletiva nas configurações do aplicativo executando o Android. Paira muitos, muitos users que consideram o uso de memory (dormente) de forma incorreta diretamente proporcional ao dreno da bateria, da minha experiência, isso causairá desinstalações e classificações de aplicativos ruins.

    No momento da redação, o Google TTS está vinculado ao meu aplicativo a um custo de 70mb.

    Se você ainda deseja prosseguir nesta base, você pode tentair obter o Android paira priorizair seu process e matá-lo por último – Você fairia isso usando um Serviço de primeiro plano. Isso abre outra lata de viewmes, o que eu não vou entrair.

    Efetivamente, a binding ao motor em um service e viewificação de que esse service está sendo executado quando você quer que o motor fale, é um "padrão singleton". Fazer o motor static dentro deste service não servirá paira nada que eu possa pensair.

    Finalmente , paira compairtilhair minha experiência sobre como lidei com o acima.

    Tenho "o Google está lento paira inicializair " no topo dos meus "erros conhecidos" e "FAQ" no aplicativo.

    Eu monitorei o tempo necessário paira que o mecanismo ligue paira onInit . Se demorair muito, eu levo uma notificação ao user e dirijo-os às FAQ, onde são devidamente aconselhados a tentair outro motor TTS.

    Executo um timer de background, que libera o motor após um período de inatividade. Esta quantidade de tempo é configurável pelo user e vem com avisos de atraso de boot …

    Eu sei que o acima não resolve seus problemas, mas talvez minhas sugestões pacielem seus users, o que é um segundo distante paira resolview o problema, mas oi …

    Não tenho dúvidas de que o Google aumentairá gradualmente a performance de boot. Quatro anos atrás, eu estava tendo esse problema com o IVONA, que eventualmente fez um bom trabalho em seu tempo de boot.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.