Android introduced text to speech tools since its begining which we can use to speech the text in different language.
In this example, we are going to discuss below given things:
To intialize implement TextToSpeech.OnInitListener, and override onInit method of TextToSpeech.OnInitListener as follows:
Call the speak method TextToSpeech object as follows:
You can call setLanguage() method of TextToSpeech object to set the language of speak. A lot of languages like Canada, French, Chinese, Germany, Hindi etc. are supported in TextToSpeech.
You can set pitch rate of TextToSpeech by calling setPitch() method of TextToSpeech object. By default, the value is 1.0 which. To decrease pitch rate just lower the values from 1.0 while to increase the pitch rate use greater value of pitch rate than 1.
The default value of speech rate is 1.0. To increase speach rate use greater value than 1 and to lower the speech rate decrease the value than 1.0
Step 1: Create a new project in Android Studio and name it Text to speech.
Step 2: Open res -> layout -> activity_main.xml and add following code
Step 3: Open your MainActivity.java and replace it source with source of my MainActivity.java as follows:
Step 4: Clean and run the project to see the result.