Home | History | Annotate | Download | only in tts
      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         if (mTextToSpeech != null) {
     43             mTextToSpeech.stop();
     44         }
     45     }
     46 
     47     @Override
     48     public boolean isSpeaking() {
     49         return mTextToSpeech != null ? mTextToSpeech.isSpeaking() : false;
     50     }
     51 
     52     @Override
     53     public void shutdown() {
     54         mTextToSpeech.shutdown();
     55         mTextToSpeech = null;
     56     }
     57 
     58     @Override
     59     public int getStream() {
     60         return TextToSpeech.Engine.DEFAULT_STREAM;
     61     }
     62 }
     63