Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

72
LINES

< > BotCompany Repo | #1001253 // Android speech output (good version)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (145L/2K/5K).

!747

import android.app.Activity;
import android.speech.tts.*;

m {
  static Activity androidContext;
  static TextToSpeech tts;
  static S text = "Hello Android";
  
  p {
    if (args.length != 0 && args[0].length() != 0) text = join(" ", args);
    
    tts = new TextToSpeech(androidContext, new TextToSpeech.OnInitListener() {
      public void onInit(int status) {
        try {
        if (status != TextToSpeech.SUCCESS)
          fail("Speech engine initialization failed");

       /* int result = tts.setLanguage(Locale.US);
        if (result == TextToSpeech.LANG_MISSING_DATA
          || result == TextToSpeech.LANG_NOT_SUPPORTED)
          fail("English is not supported");
       */
       
        int result = tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
          @Override
          public void onDone(String utteranceId)
          {
            print("onDone");
            done();
          }

          @Override
          public void onError(String utteranceId) {
            print("onError");
          }

          @Override
          public void onStart(String utteranceId) {
            print("onStart");
          }
        });
        if (result != TextToSpeech.SUCCESS)
          print("Could not set listener");
            
        speak();
        /*thread {
          sleepSeconds(20); // to be improved :) (OnUtteranceListener)
          androidContext.runOnUiThread(runnable {
            done();
          });
        }*/
        } catch (Throwable e) { e.printStackTrace(); }
      }
    });
  }
  
  static void speak() {
    print(text);
    new HashMap params;
    params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "UniqueID");
    tts.speak(text, TextToSpeech.QUEUE_FLUSH, params);
  }
  
  static void done() {
    print("Closing TTS");
    tts.stop();
    tts.shutdown();
    tts = null;
  }
}

Author comment

Began life as a copy of #1001175

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, ufhdzwgrioxs, vouqrxazstgt

Comments [hide]

ID Author/Program Comment Date
1114 stefan Now it is! :) 2015-10-05 14:53:19
1113 stefan Listener is not called... 2015-10-05 14:48:17

add comment

Snippet ID: #1001253
Snippet name: Android speech output (good version)
Eternal ID of this version: #1001253/1
Text MD5: 0694e187190b593f5f7a52a941e8749d
Transpilation MD5: 9f42f3b1d9dc7bf708a1072df7fbdd85
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-10-07 21:07:26
Source code size: 1939 bytes / 72 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 922 / 908
Referenced in: [show references]