1 package com.android.car.messenger.tts; 2 3 import android.content.Context; 4 import android.os.Bundle; 5 import android.speech.tts.TextToSpeech; 6 import android.speech.tts.UtteranceProgressListener; 7 8 /** 9 * Implementation of {@link TTSEngine} that delegates to Android's {@link TextToSpeech} API. 10 * <p> 11 * NOTE: {@link #initialize(Context, TextToSpeech.OnInitListener)} must be called to use this 12 * engine. After {@link #shutdown()}, {@link #initialize(Context, TextToSpeech.OnInitListener)} may 13 * be called again to use it again. 14 */ 15 class AndroidTTSEngine implements TTSEngine { 16 private TextToSpeech mTextToSpeech; 17 18 @Override 19 public void initialize(Context context, TextToSpeech.OnInitListener initListener) { 20 if (mTextToSpeech == null) { 21 mTextToSpeech = new TextToSpeech(context, initListener); 22 } 23 } 24 25 @Override 26 public boolean isInitialized() { 27 return mTextToSpeech != null; 28 } 29 30 @Override 31 public void setOnUtteranceProgressListener(UtteranceProgressListener progressListener) { 32 mTextToSpeech.setOnUtteranceProgressListener(progressListener); 33 } 34 35 @Override 36 public int speak(CharSequence text, int queueMode, Bundle params, String utteranceId) { 37 return mTextToSpeech.speak(text, queueMode, params, utteranceId); 38 } 39 40 @Override 41 public void stop() { 42 mTextToSpeech.stop(); 43 } 44 45 @Override 46 public boolean isSpeaking() { 47 return mTextToSpeech != null ? mTextToSpeech.isSpeaking() : false; 48 } 49 50 @Override 51 public void shutdown() { 52 mTextToSpeech.shutdown(); 53 mTextToSpeech = null; 54 } 55 } 56