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

57
LINES

< > BotCompany Repo | #1004119 // androidSay

JavaX fragment (include)

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

sbool androidSay_keepEngine = false;

// locale = language, e.g. Locale.GERMAN
static void androidSay(final S text, final Locale locale) {
  if (androidSay_keepEngine) {
    androidSay_keepEngine(text, locale);
    ret;
  }
  
  final new Flag flag;
  
  final TextToSpeech[] tts = new TextToSpeech[1];
  tts[0] = new TextToSpeech(androidContext(), new TextToSpeech.OnInitListener() {
    public void onInit(int status) {
      try {
        if (status != TextToSpeech.SUCCESS)
          fail("Speech engine initialization failed");
  
        int result = tts[0].setLanguage(locale);
        if (result == TextToSpeech.LANG_MISSING_DATA
          || result == TextToSpeech.LANG_NOT_SUPPORTED)
          fail("Language is not supported");
  
        result = tts[0].setOnUtteranceProgressListener(new UtteranceProgressListener() {
          public void onDone(String utteranceId) {
            print("TTS done");
            tts[0].shutdown();
            flag.raise();
          }
  
          public void onError(String utteranceId) {
            print("TTS error");
            tts[0].shutdown();
            flag.raise();
          }
  
          public void onStart(String utteranceId) {
          }
        });
        
        if (result != TextToSpeech.SUCCESS)
          print("Could not set listener");
            
        print(text);
        new HashMap params;
        params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, randomID(8));
        tts[0].speak(text, TextToSpeech.QUEUE_FLUSH, params);
      
      } catch (Throwable e) { e.printStackTrace(); flag.raise(); }
    }
  });
    
  flag.waitUntilUp();
}

Author comment

Began life as a copy of #1001301

download  show line numbers  debug dex  old transpilations   

Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, sawdedvomwva, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1004119
Snippet name: androidSay
Eternal ID of this version: #1004119/2
Text MD5: ec122d4debd55a98c98b949a795130e2
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-02-16 18:14:03
Source code size: 1745 bytes / 57 lines
Pitched / IR pitched: No / No
Views / Downloads: 654 / 659
Version history: 1 change(s)
Referenced in: [show references]