| 203 | Log.i("TTS init"); |
| 204 | instance = new TextToSpeech(ServiceTTS.this, new TextToSpeech.OnInitListener() { |
| 205 | @Override |
| 206 | public void onInit(int initStatus) { |
| 207 | instance.setOnUtteranceProgressListener(new UtteranceProgressListener() { |
| 208 | @Override |
| 209 | public void onStart(String utteranceId) { |
| 210 | Log.i("TTS start=" + utteranceId); |
| 211 | try { |
| 212 | startForeground(NotificationHelper.NOTIFICATION_TTS, getNotification(utteranceId)); |
| 213 | } catch (Throwable ex) { |
| 214 | if (Helper.isPlayStoreInstall()) |
| 215 | Log.i(ex); |
| 216 | else |
| 217 | Log.e(ex); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public void onDone(String utteranceId) { |
| 223 | Log.i("TTS done=" + utteranceId); |
| 224 | report(utteranceId); |
| 225 | synchronized (lock) { |
| 226 | if (!instance.isSpeaking()) |
| 227 | try { |
| 228 | Log.i("TTS shutdown"); |
| 229 | instance.shutdown(); |
| 230 | } catch (Throwable ex) { |
| 231 | Log.e(ex); |
| 232 | } finally { |
| 233 | status = null; |
| 234 | instance = null; |
| 235 | ServiceTTS.this.stopSelf(); |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | @Override |
| 241 | public void onError(String utteranceId) { |
| 242 | Log.i("TTS error=" + utteranceId); |
| 243 | report(utteranceId); |
| 244 | } |
| 245 | |
| 246 | private void report(String utteranceId) { |
| 247 | lbm.sendBroadcast(new Intent(ACTION_TTS_COMPLETED) |
| 248 | .putExtra(EXTRA_UTTERANCE_ID, utteranceId)); |
| 249 | } |
| 250 | }); |
| 251 | |
| 252 | synchronized (lock) { |
| 253 | status = initStatus; |
| 254 | Log.i("TTS status=" + status + " queued=" + queue.size()); |
| 255 | if (status == TextToSpeech.SUCCESS) |
| 256 | for (Runnable speak : queue) |
| 257 | speak.run(); |
| 258 | queue.clear(); |
| 259 | } |
| 260 | } |
| 261 | }); |
| 262 | } catch (Throwable ex) { |