Text to speech in Android
Hi friends, Today we are going to develop an Android application - Text to speech converter.
Lets start, Open your ADT Bundle(Eclipse),
Go to File -- New -- Android Application Project . Name the project TextToSpeech. Go on clicking until finish button comes.
Go to File -- New -- Android Application Project . Name the project TextToSpeech. Go on clicking until finish button comes.
In activity_main.xml. Copy the following code.
Graphical Layout of activity_main.xml will look like this
Now we will open MainActivity.java file.
Import some packages into .java file.
If you refer http://developer.android.com/reference/android/speech/tts/TextToSpeech.html . i.e TextToSpeech class in developer.android documentation, they told two things.
So, first of all implement TextToSpeech.OnInitListener interface and import following packages. If you visit TextToSpeech.OnInitListener. in developers.android. You will notice one abstract method. Short meaning of abstract method is it have to define/implement in subclass. So we will also implement onInit() method in MainActivity.java
Before that just import following packages:
import java.util.Locale;
import android.speech.tts.TextToSpeech;
Implementing TextToSpeech.OnInitListener.
Now we will create references of 3 classes
- Button class
- EditText class
- TextToSpeech class
Now we will write few sentences,
- We will initialize tts reference by coping its own object.
- call method findViewById for button and EditText and we will set their id's.(Refer R.java and .xml)
- We will call setOnClickListener method for b object. setOnClickListener method needs View.OnClickListener Object which is an interface hence we cant directly create object and pass, We will use anonymous class concept . If we visit View.OnClickListener then we will find one absstract method onClick().
We have nothing to do with view object, hence we won't go in details of view. Whatever action we have to take after clicking button will be written in onClick() method. hence we just call user defined start() method. We will define it later.
Now we will write init() method.
Local is a class of java.util.Local and US feild is static hence we access it directly by class name. setEnabled() is button class method which requires Boolean value
Now we will write start() method.
getText() method is of EditText class and it returns string value. hence we call toString() method to get output in String format. speak() method is of TextToSpeech class. it returns int value but we have nothing to do with that value. hence we are not taking it. speak() has 3 parameters.
- String (Which we have stored in text in previous line)
- integer queueMode (Constant integer value provided by TextToSpeech class)
- Hashmap (NULL)
Now our next step is to call shutdown() method.
Now we will write onDestroy method().
![]() |
Now our project is ready. Its time to run it. Open your AVD . Go in Windows--Android Virtual Device Manager-- Start AVD.(If created otherwise you have to create it by going Windows--Android Virtual Device Manager)
Now right click on your project and run as Android Application. In emulator type the input in EditText and click start to listen the output
***** Thank You *****















No comments:
Post a Comment