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

72
LINES

< > BotCompany Repo | #1004121 // androidSay_keepEngine

JavaX fragment (include)

1  
scope androidSay_keepEngine.
2  
3  
import android.app.Activity;
4  
import android.speech.tts.*;
5  
6  
sbool #verbose, #inCall;
7  
static TextToSpeech #tts;
8  
9  
// TODO: better way to sync than synchronizing on main class
10  
11  
static synchronized void androidSay_keepEngine(final S text, final Locale locale) {
12  
  final new Flag flag;
13  
  
14  
  if (tts == null) {
15  
    if (verbose) print("Initialising TTS");
16  
    tts = new TextToSpeech(androidContext(), new TextToSpeech.OnInitListener() {
17  
      public void onInit(int status) {
18  
        try {
19  
          if (status != TextToSpeech.SUCCESS)
20  
            fail("Speech engine initialization failed");
21  
    
22  
          go(text, locale, flag);
23  
        } catch (Throwable e) { e.printStackTrace(); flag.raise(); }
24  
      }
25  
    });
26  
    if (verbose) print("TTS object created");
27  
  } else
28  
    androidSay_keepEngine_go(text, locale, flag);
29  
    
30  
  flag.waitUntilUp();
31  
}
32  
33  
static void #go(S text, Locale locale, final Flag flag) {
34  
  if (verbose) print("TTS go: " + text);
35  
  int result = tts.setLanguage(locale);
36  
  if (result == TextToSpeech.LANG_MISSING_DATA
37  
    || result == TextToSpeech.LANG_NOT_SUPPORTED)
38  
    fail("German is not supported");
39  
    
40  
  /*if (empty(text)) { // Empty text = Initialize engine only
41  
    flag.raise();
42  
    ret;
43  
  }*/
44  
45  
  result = tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
46  
    public void onDone(String utteranceId) {
47  
      print("TTS done");
48  
      flag.raise();
49  
    }
50  
51  
    public void onError(String utteranceId) {
52  
      print("TTS error");
53  
      flag.raise();
54  
    }
55  
56  
    public void onStart(String utteranceId) {
57  
      if (verbose) print("TTS started");
58  
    }
59  
  });
60  
  
61  
  if (result != TextToSpeech.SUCCESS)
62  
    print("Could not set listener");
63  
      
64  
  //if (nempty(text)) print("[speech output] " + text);
65  
  new HashMap params;
66  
  params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, randomID(8));
67  
  if (inCall)
68  
    params.put(TextToSpeech.Engine.KEY_PARAM_STREAM, str(AudioManager.STREAM_VOICE_CALL));
69  
  tts.speak(text, TextToSpeech.QUEUE_FLUSH, params);
70  
}
71  
72  
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: 583 / 592
Version history: 6 change(s)
Referenced in: [show references]