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

57
LINES

< > BotCompany Repo | #1004119 // androidSay

JavaX fragment (include)

1  
import android.app.Activity;
2  
import android.speech.tts.*;
3  
4  
sbool androidSay_keepEngine = false;
5  
6  
// locale = language, e.g. Locale.GERMAN
7  
static void androidSay(final S text, final Locale locale) {
8  
  if (androidSay_keepEngine) {
9  
    androidSay_keepEngine(text, locale);
10  
    ret;
11  
  }
12  
  
13  
  final new Flag flag;
14  
  
15  
  final TextToSpeech[] tts = new TextToSpeech[1];
16  
  tts[0] = 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  
        int result = tts[0].setLanguage(locale);
23  
        if (result == TextToSpeech.LANG_MISSING_DATA
24  
          || result == TextToSpeech.LANG_NOT_SUPPORTED)
25  
          fail("Language is not supported");
26  
  
27  
        result = tts[0].setOnUtteranceProgressListener(new UtteranceProgressListener() {
28  
          public void onDone(String utteranceId) {
29  
            print("TTS done");
30  
            tts[0].shutdown();
31  
            flag.raise();
32  
          }
33  
  
34  
          public void onError(String utteranceId) {
35  
            print("TTS error");
36  
            tts[0].shutdown();
37  
            flag.raise();
38  
          }
39  
  
40  
          public void onStart(String utteranceId) {
41  
          }
42  
        });
43  
        
44  
        if (result != TextToSpeech.SUCCESS)
45  
          print("Could not set listener");
46  
            
47  
        print(text);
48  
        new HashMap params;
49  
        params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, randomID(8));
50  
        tts[0].speak(text, TextToSpeech.QUEUE_FLUSH, params);
51  
      
52  
      } catch (Throwable e) { e.printStackTrace(); flag.raise(); }
53  
    }
54  
  });
55  
    
56  
  flag.waitUntilUp();
57  
}

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: 655 / 660
Version history: 1 change(s)
Referenced in: [show references]