Kotlin Text to Speech Application

Kotlin Text to Speech Application | Working Example Kotlin

In this Tutorial I will explain how you can build a Kotlin Text to Speech Application in Android Studio using Kotlin Language. You write any text in the text box and the application will speak that text on just one click 🙂 . So, lets start our Kotlin Text to Speech Application.

What is Android Text to Speech:

TextToSpeech class in Android converts the Text into Speech. It converts the given text into speech form. After using this class use its method shutdown() to stop it completely.

Kotlin Text to Speech Application:

1- Start a new Android Studio Project and name it as “Text_To_Speech_New” or you can name it whatever you want. Select API level 19 and default language should be Kotlin.

2- After that, go to activity_main.xml and change the default Constraint Layout to Relative Layout.

3- So, in your activity_main.xml file you need some views. A Text View, Edit Text, Button and a Seek bar.

4- We are using Edit Text to write the text, Button to Listen the Speech, and then a Seek bar to setup the speed and pitch of the sound and finally two TextView(s) to display the Pitch and Speed.

5- After setting up all these views our final activity_main.xml will look like this.

activity_main.xml

 

MainActivity.kt Code

6- Now, its the time to play with our Kotlin code 🙂 …

7- So, go to your MainActivity.kt file and after that first define all the views you are using in your XML.

8- We initialize all these views in a separate method called ” initializeFields() ” . Our method will look like this.

9- After this we will call this method in our onCreate method of the Main Activity.

10- Now, in our onCreate method we will initialize and setup an OnItListener on our TextToSpeech.

11- Now we will perform every different task in a separate Method. We need three methods here. One to Enable speech, another one to stop the speech once the Activity destroys and one method to close the keyboard after writing the text in our text box.

12- In speak() method we will define the speak functionality and then setup the Pitch and Speed of our spoken text.

13- Our onDestroy() method will end the speech if the speech is in progress and the user closes the application. Our method will look like this.

14- After writing the text in our text box when we click the button to listen the text in audio this method will close our soft keyboard.

15- So, our final complete MainActivity.kt file will look like this.

 

 

Here you go, Enter any text in the Text Box and press the Button. The Text_To_Speech functionality will speak your text . Enjoy 🙂 … 

 

OUTPUT

 

 

Kotlin Text to Speech Application
Kotlin Text to Speech Application

 

Kotlin Text to Speech Application
Kotlin Text to Speech Application

 

More Android Examples:

4 thoughts on “Kotlin Text to Speech Application | Working Example Kotlin”

  1. Pingback: Kotlin Speech To Text Example | Step by Step Example - My Android

  2. Pingback: ListView In Android Using Kotlin: ListView Example in Kotlin - My Android

  3. Pingback: Android Indeterminate Progress Bar: How to create Progress Bar

  4. Pingback: Enable or Disable Wifi In Android Programmatically In Kotlin - My Android

Leave a Comment

Your email address will not be published. Required fields are marked *