'Class module
Sub Class_Globals
Private nativeMe As JavaObject
End Sub
'Initializes the object.
Public Sub Initialize
nativeMe = Me
nativeMe.RunMethod("Initialize", Null)
End Sub
'Speak out a string of characters (a word or a sentence)
'Example:
'Dim tts As TTSUtilities
'tts.Initialize
'
'Dim mystring As String = "This is a TTS message"
'tts.speakOut(mystring)
Public Sub Speak (mystring As String)
nativeMe.RunMethod("speakOut", Array (mystring))
End Sub
'Release the TextToSpeech resources
'Example:
'Dim tts As TTSUtilities
'tts.Initialize
'
'tts.clearTextToSpeech
Public Sub clearTextToSpeech
nativeMe.RunMethod("clearTextToSpeech", Null)
End Sub
Private Sub tts_finished
CallSubDelayed(Main, "ttsFinished")
End Sub
Private Sub tts_error
Log("In B4A and TTS error")
End Sub
Private Sub tts_started
CallSubDelayed(Main, "ttsStarted")
End Sub
#If Java
import android.speech.tts.TextToSpeech;
import java.util.Locale;
import android.speech.tts.UtteranceProgressListener;
private TextToSpeech tts;
public void Initialize() {
tts=new TextToSpeech(BA.applicationContext, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
//BA.Log("STATUS = " + status);
if (status == TextToSpeech.SUCCESS) {
tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onDone(String utteranceId) {
//BA.Log("TTS finished");
//BA.Log(" ");
ba.raiseEventFromUI(this, "tts_finished", null);
}
@Override
public void onError(String utteranceId) {
//BA.Log("TTS error");
//BA.Log(" ");
ba.raiseEventFromUI(this, "tts_error", null);
}
@Override
public void onStart(String utteranceId) {
//BA.Log("TTS start");
//BA.Log(" ");
ba.raiseEventFromUI(this, "tts_started", null);
}
});
} else {
BA.Log("Initilization Failed!");
}
}
});
}
public void speakOut(String str) {
tts.speak(str, TextToSpeech.QUEUE_ADD, null, "message");
}
public void clearTextToSpeech() {
// Don't forget to shutdown tts!
if (tts != null) {
tts.stop();
tts.shutdown();
}
}
#End If