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

72
LINES

< > BotCompany Repo | #1004121 // androidSay_keepEngine

JavaX fragment (include)

scope androidSay_keepEngine.

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

sbool #verbose, #inCall;
static TextToSpeech #tts;

// TODO: better way to sync than synchronizing on main class

static synchronized void androidSay_keepEngine(final S text, final Locale locale) {
  final new Flag flag;
  
  if (tts == null) {
    if (verbose) print("Initialising TTS");
    tts = new TextToSpeech(androidContext(), new TextToSpeech.OnInitListener() {
      public void onInit(int status) {
        try {
          if (status != TextToSpeech.SUCCESS)
            fail("Speech engine initialization failed");
    
          go(text, locale, flag);
        } catch (Throwable e) { e.printStackTrace(); flag.raise(); }
      }
    });
    if (verbose) print("TTS object created");
  } else
    androidSay_keepEngine_go(text, locale, flag);
    
  flag.waitUntilUp();
}

static void #go(S text, Locale locale, final Flag flag) {
  if (verbose) print("TTS go: " + text);
  int result = tts.setLanguage(locale);
  if (result == TextToSpeech.LANG_MISSING_DATA
    || result == TextToSpeech.LANG_NOT_SUPPORTED)
    fail("German is not supported");
    
  /*if (empty(text)) { // Empty text = Initialize engine only
    flag.raise();
    ret;
  }*/

  result = tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
    public void onDone(String utteranceId) {
      print("TTS done");
      flag.raise();
    }

    public void onError(String utteranceId) {
      print("TTS error");
      flag.raise();
    }

    public void onStart(String utteranceId) {
      if (verbose) print("TTS started");
    }
  });
  
  if (result != TextToSpeech.SUCCESS)
    print("Could not set listener");
      
  //if (nempty(text)) print("[speech output] " + text);
  new HashMap params;
  params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, randomID(8));
  if (inCall)
    params.put(TextToSpeech.Engine.KEY_PARAM_STREAM, str(AudioManager.STREAM_VOICE_CALL));
  tts.speak(text, TextToSpeech.QUEUE_FLUSH, params);
}

end scope

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1004121
Snippet name: androidSay_keepEngine
Eternal ID of this version: #1004121/7
Text MD5: 6bd2fef47e8d79a2e3cface4a814af86
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:25:41
Source code size: 2098 bytes / 72 lines
Pitched / IR pitched: No / No
Views / Downloads: 652 / 662
Version history: 6 change(s)
Referenced in: [show references]