init TextToSpeech - Android android.speech.tts

Android examples for android.speech.tts:TextToSpeech

Description

init TextToSpeech

Demo Code


import android.app.Activity;
import android.os.Build;
import android.speech.tts.TextToSpeech;
import android.widget.Toast;

public class Main {
  private static TextToSpeech ttsInstance;
  private static TextToSpeech.OnInitListener listener;

  private static void initListener(final Activity context) {
    listener = new TextToSpeech.OnInitListener() {
      @Override/*from   w  ww.  ja  v a2 s.  c  o m*/
      public void onInit(int status) {
        if (status == TextToSpeech.ERROR) {
          Toast.makeText(context, "Error ao inicializar TTS...",
              Toast.LENGTH_LONG).show();
          ttsInstance = null;
        } else {
          playTTSAdvertisement(context);
        }
      }
    };
  }

  private static void playTTSAdvertisement(Activity context) {
    ttsInstance.setLanguage(context.getResources().getConfiguration().locale);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      ttsInstance.speak("driver_advise", TextToSpeech.QUEUE_FLUSH, null,
          "REQUEST_TTS");
    } else {
      ttsInstance.speak("asdf", TextToSpeech.QUEUE_FLUSH, null);
    }
  }
}

Related Tutorials