Saturday, July 16, 2011

Using the official TextToSpeech in Android 1.6 tutorial

Android 1.6 SDK had been released and previously if you want TTS on your app, you have to use the one from eyes-free, while this one was the whole inspiration of having TTS by default on android, an official API is official. lol



Main


public class Main extends Activity implements TextToSpeech.OnInitListener {
  TextToSpeech tts;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tts = new TextToSpeech(this,this);
  }

  public void onInit(int status) {
    Locale loc = new Locale("es", "","");
    if(tts.isLanguageAvailable(loc) >= TextToSpeech.LANG_AVAILABLE){
      tts.setLanguage(loc);
    }
    tts.speak("hola mundo", TextToSpeech.QUEUE_FLUSH, null);
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    tts.shutdown();
  }

No comments:

Post a Comment