-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTTS.java
More file actions
156 lines (136 loc) · 4.83 KB
/
TTS.java
File metadata and controls
156 lines (136 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package anywheresoftware.b4a.obejcts;
import java.util.Locale;
import java.util.Set;
import anywheresoftware.b4a.BA;
import android.speech.tts.Voice;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.EngineInfo;
import android.speech.tts.UtteranceProgressListener;
import anywheresoftware.b4a.AbsObjectWrapper;
import anywheresoftware.b4a.objects.collections.List;
@BA.Version(1.2f)
@BA.ShortName("TTS")
public class TTS extends AbsObjectWrapper<TextToSpeech> {
private BA _ba = null;
private String _EventName = null;
public void Initialize(final BA ba, final String EventName) {
_ba = ba;
_EventName = EventName;
final TextToSpeech tts = new TextToSpeech(ba.context, (TextToSpeech.OnInitListener)new TextToSpeech.OnInitListener() {
public void onInit(final int status) {
ba.raiseEventFromUI((Object)null, String.valueOf(EventName.toLowerCase(BA.cul)) + "_ready", new Object[] { status == 0 });
}
});
setListerner(tts);
this.setObject(tts);
}
private void Initialize(final BA ba, final String EventName, final String CustomEngine) {
final TextToSpeech tts = new TextToSpeech(ba.context, (TextToSpeech.OnInitListener)new TextToSpeech.OnInitListener() {
public void onInit(final int status) {
ba.raiseEventFromUI((Object)null, String.valueOf(EventName.toLowerCase(BA.cul)) + "_ready", new Object[] { status == 0 });
}
}, CustomEngine);
setListerner(tts);
this.setObject(tts);
}
public int Speak(final String Text, final boolean ClearQueue, final String UtteranceId) {
final int r = ((TextToSpeech)this.getObject()).speak(Text, (int)(ClearQueue ? 0 : 1), null, UtteranceId);
return r;
}
public void Stop() {
((TextToSpeech)this.getObject()).stop();
}
public void setPitch(final float value) {
((TextToSpeech)this.getObject()).setPitch(value);
}
public void setSpeechRate(final float value) {
((TextToSpeech)this.getObject()).setSpeechRate(value);
}
public boolean SetLanguage(final String Language, final String Country) {
Locale l;
if (Country.length() > 0) {
l = new Locale(Language, Country);
}
else {
l = new Locale(Language);
}
final int r = ((TextToSpeech)this.getObject()).setLanguage(l);
return r >= 0;
}
public boolean isSpeaking() {
return ((TextToSpeech)this.getObject()).isSpeaking();
}
public List GetVoices()
{
Set<Voice> voices = ((TextToSpeech)this.getObject()).getVoices();
List voicesList = new List();
voicesList.Initialize();
for(Voice eachVoice : voices)
{
voicesList.Add(eachVoice.getName());
}
return voicesList;
}
public boolean SetVoice(final String VoiceName)
{
Set<Voice> voices = ((TextToSpeech)this.getObject()).getVoices();
for (Voice eachVoice : voices) {
if (eachVoice.getName().equalsIgnoreCase(VoiceName))
{
return (((TextToSpeech)this.getObject()).setVoice(eachVoice) == 0);
}
}
return false;
}
public List GetEngines()
{
java.util.List<EngineInfo> engines = ((TextToSpeech)this.getObject()).getEngines();
List enginesList = new List();
enginesList.Initialize();
for(EngineInfo eachEngine : engines)
{
enginesList.Add(eachEngine.name);
}
return enginesList;
}
public boolean SetEngine(final String EngineName)
{
if (_EventName != null)
{
java.util.List<EngineInfo> engines = ((TextToSpeech)this.getObject()).getEngines();
for (EngineInfo eachEngine : engines) {
if (eachEngine.name.equalsIgnoreCase(EngineName))
{
((TextToSpeech)this.getObject()).shutdown();
Initialize(_ba, _EventName, EngineName);
return true;
}
}
}
return false;
}
public void Release() {
if (this.IsInitialized()) {
((TextToSpeech)this.getObject()).shutdown();
this.setObject(null);
}
}
private void setListerner(TextToSpeech tts)
{
if (tts.getEngines().size() > 0)
{
tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onDone(String utteranceId) {
_ba.raiseEventFromUI((Object)null, String.valueOf(_EventName.toLowerCase(BA.cul)) + "_finish", new Object[] { utteranceId });
}
@Override
public void onError(String utteranceId) {
}
@Override
public void onStart(String utteranceId) {
}
});
}
}
}