Code example for TextToSpeech

Methods: setOnUtteranceCompletedListener, synthesizeToFile

0
    tts = new TextToSpeech(this, new OnInitListener() {
 
      @Override 
      public void onInit(int status) {
        // TODO Auto-generated method stub 
        tts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener() {
          @Override public void onUtteranceCompleted(String utteranceId) {
            d("done " + utteranceId + " isSpeaking=" + tts.isSpeaking());
            tts.stop();
            //Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", ); 
            go(); 
          } 
        }); 
        go(); 
      } 
    }); 
  } 
 
  private void go() { 
    if (curText < texts.length) {
      HashMap<String, String> params = new HashMap<String, String>();
      params.put(Engine.KEY_PARAM_UTTERANCE_ID, "" + curText);
      File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
      dir.mkdirs();
      d("synthesizing " + texts[curText].substring(0,50) + "...");
      if (tts.synthesizeToFile(texts[curText], params, dir + "/tts" + curText + ".wav") != TextToSpeech.SUCCESS) {
        throw new RuntimeException();
      } 
      curText++;
    } else { 
      d("finished");