2019-12-30 18:57:19 +00:00
|
|
|
package games.lightsout.godot.tts;
|
|
|
|
|
2020-05-28 17:05:21 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
2020-06-02 21:23:45 +00:00
|
|
|
import org.godotengine.godot.Godot;
|
|
|
|
|
2020-05-28 17:05:21 +00:00
|
|
|
import android.accessibilityservice.AccessibilityServiceInfo;
|
2019-12-30 18:57:19 +00:00
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.speech.tts.TextToSpeech;
|
2020-05-28 17:05:21 +00:00
|
|
|
import android.view.accessibility.AccessibilityManager;
|
|
|
|
|
2019-12-30 18:57:19 +00:00
|
|
|
public class TTS extends Godot.SingletonBase implements TextToSpeech.OnInitListener {
|
|
|
|
|
|
|
|
protected Activity appActivity;
|
|
|
|
protected Context appContext;
|
|
|
|
private Godot activity = null;
|
|
|
|
private int instanceId = 0;
|
|
|
|
|
|
|
|
private TextToSpeech tts = null;
|
|
|
|
|
2020-05-28 17:05:21 +00:00
|
|
|
private float rate = 1f;
|
|
|
|
|
2019-12-30 18:57:19 +00:00
|
|
|
private Integer utteranceId = 0;
|
|
|
|
|
|
|
|
public void speak(String text, boolean interrupt) {
|
|
|
|
int mode = TextToSpeech.QUEUE_ADD;
|
|
|
|
if (interrupt)
|
|
|
|
mode = TextToSpeech.QUEUE_FLUSH;
|
|
|
|
tts.speak(text, mode, null, this.utteranceId.toString());
|
|
|
|
this.utteranceId++;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void stop() {
|
|
|
|
tts.stop();
|
|
|
|
}
|
|
|
|
|
2020-05-19 13:10:02 +00:00
|
|
|
public float get_rate() {
|
2020-05-28 17:05:21 +00:00
|
|
|
return this.rate;
|
2020-05-19 13:10:02 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 17:29:44 +00:00
|
|
|
public void set_rate(float rate) {
|
2020-05-28 17:05:21 +00:00
|
|
|
this.rate = rate;
|
2020-05-19 13:10:02 +00:00
|
|
|
tts.setSpeechRate(rate);
|
2020-05-17 17:46:57 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 21:23:45 +00:00
|
|
|
public boolean is_speaking() {
|
|
|
|
return tts.isSpeaking();
|
|
|
|
}
|
|
|
|
|
2020-05-28 17:05:21 +00:00
|
|
|
public boolean has_screen_reader() {
|
|
|
|
AccessibilityManager accessibilityManager = (AccessibilityManager) appContext
|
|
|
|
.getSystemService(Context.ACCESSIBILITY_SERVICE);
|
|
|
|
if (accessibilityManager != null) {
|
2020-06-02 21:23:45 +00:00
|
|
|
List<AccessibilityServiceInfo> screenReaders = accessibilityManager
|
|
|
|
.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_SPOKEN);
|
2020-05-28 17:05:21 +00:00
|
|
|
return screenReaders.size() != 0;
|
2020-06-02 21:23:45 +00:00
|
|
|
} else {
|
2020-05-28 17:05:21 +00:00
|
|
|
return false;
|
2020-06-02 21:23:45 +00:00
|
|
|
}
|
2020-05-28 17:05:21 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 18:57:19 +00:00
|
|
|
public void getInstanceId(int pInstanceId) {
|
2020-05-17 17:46:57 +00:00
|
|
|
// You will need to call this method from Godot and pass in the
|
|
|
|
// get_instance_id().
|
2019-12-30 18:57:19 +00:00
|
|
|
instanceId = pInstanceId;
|
|
|
|
}
|
|
|
|
|
|
|
|
static public Godot.SingletonBase initialize(Activity p_activity) {
|
|
|
|
return new TTS(p_activity);
|
|
|
|
}
|
|
|
|
|
|
|
|
public TTS(Activity p_activity) {
|
2020-05-17 17:46:57 +00:00
|
|
|
this.activity = (Godot) p_activity;
|
2019-12-30 18:57:19 +00:00
|
|
|
this.appActivity = p_activity;
|
|
|
|
this.appContext = appActivity.getApplicationContext();
|
|
|
|
this.tts = new TextToSpeech(this.appContext, this);
|
|
|
|
// Register class name and functions to bind.
|
2020-06-02 21:23:45 +00:00
|
|
|
registerClass("AndroidTTS", new String[] { "speak", "stop", "get_rate", "set_rate", "has_screen_reader",
|
|
|
|
"is_speaking", "getInstanceId" });
|
2019-12-30 18:57:19 +00:00
|
|
|
this.activity.runOnUiThread(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onInit(int status) {
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|