Using TextToSpeech in Sketchware

To know how to use of TextToSpeech class in Sketchware, for reading the text from an EditText field, follow the instructions given below.

1. In your Sketchware Android project, and an EditText edittext1, and a Button button1.

2. In LOGIC area, in onCreate event, use add source directly block and in the block put the code given below. It should be the last block in onCreate event.
t1=new android.speech.tts.TextToSpeech(getApplicationContext(), new android.speech.tts.TextToSpeech.OnInitListener() {
@Override public void onInit(int status) {
if(status != android.speech.tts.TextToSpeech.ERROR) {
t1.setLanguage(Locale.UK); } } });
}android.speech.tts.TextToSpeech t1;
private void nothing(){

This code initializes the TextToSpeech engine. It can speak only after initialization. If the user performs some other Activity after initialization, then it will not speak. It has to be initialized again for it to speak. So place this code again at appropriate place so that it is initialized before the speak function is executed.

3. In button1 onClick event, create a String variable text, and set this variable to the contents of edittext1.

4. After this use add source directly block and put following code in it.
t1.speak(text, android.speech.tts.TextToSpeech.QUEUE_FLUSH, null);

In this code text is the String variable which is read using TextToSpeech.

5. Then add a new event onPause. In onPause event use add source directly block and put following code in it.
if(t1 !=null){
t1.stop();
t1.shutdown(); }
6. Save and run the project. In the app write something in EditText field and click the button to hear the app read the text.

7. To pause the TextToSpeech when a Button is clicked, use an add source directly block in onButtonClick and write following code in it.
if(t1 !=null){
t1.stop();
t1.shutdown(); }

Note(Alternative method):
If there are other actions between onCreate and speak button Click, and it is not reading on button click then the TextToSpeech engine can be initialized on speak button Click. To do that put following code in onCreate event.
}android.speech.tts.TextToSpeech t1;{

In button1 onClick event, set the string variable text to the contents of edittext1, and after that put following code in add source directly block.
t1=new android.speech.tts.TextToSpeech(getApplicationContext(), new android.speech.tts.TextToSpeech.OnInitListener() {
@Override public void onInit(int status) {
if(status != android.speech.tts.TextToSpeech.ERROR) {
t1.setLanguage(Locale.UK);
t1.speak(text, android.speech.tts.TextToSpeech.QUEUE_FLUSH, null);
} } });

And in onPause event put the same code as given in previous method above.
But since the TextToSpeech takes time to initialize, it will start speaking few seconds after button click.

A better option will be to put code for initializing TextToSpeech, at end of other actions on the page, as well as in onCreate.


Comments

  1. Replies
    1. It's not working too here but gonna try it now again to check the result of it!!

      Delete
  2. Explain the method. Video

    ReplyDelete
  3. How can I use this code for Turkish?

    ReplyDelete
  4. Replies
    1. Check this for supported languages :
      https://developer.android.com/reference/java/util/Locale.html

      Delete
  5. For Android:
    1. Settings
    2. Accessibility Option( I am not sure. I dont know the word in English. In German it's "Eingabehilfe")
    3. Text-To-Speech
    4. Change to other language

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete

Post a Comment

Popular posts from this blog

Simple car racing android game in Sketchware

Creating a Drawing View in Sketchware

Enable Fullscreen for Youtube videos in WebView

How to enable upload from webview in Sketchware?

List of Calendar Format symbols valid in Sketchware