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 initialize(Context context, TextToSpeech.OnInitListener initListener)19 public void initialize(Context context, TextToSpeech.OnInitListener initListener) { 20 if (mTextToSpeech == null) { 21 mTextToSpeech = new TextToSpeech(context, initListener); 22 } 23 } 24 25 @Override isInitialized()26 public boolean isInitialized() { 27 return mTextToSpeech != null; 28 } 29 30 @Override setOnUtteranceProgressListener(UtteranceProgressListener progressListener)31 public void setOnUtteranceProgressListener(UtteranceProgressListener progressListener) { 32 mTextToSpeech.setOnUtteranceProgressListener(progressListener); 33 } 34 35 @Override speak(CharSequence text, int queueMode, Bundle params, String utteranceId)36 public int speak(CharSequence text, int queueMode, Bundle params, String utteranceId) { 37 return mTextToSpeech.speak(text, queueMode, params, utteranceId); 38 } 39 40 @Override stop()41 public void stop() { 42 mTextToSpeech.stop(); 43 } 44 45 @Override isSpeaking()46 public boolean isSpeaking() { 47 return mTextToSpeech != null ? mTextToSpeech.isSpeaking() : false; 48 } 49 50 @Override shutdown()51 public void shutdown() { 52 mTextToSpeech.shutdown(); 53 mTextToSpeech = null; 54 } 55 } 56