Android allows to user to convert his or her text in to voice. Android provides a class named TextToSpeech for the purpose of converting text to voice. We can produce the voice output in different languages. Here I am going to show you how we can use this feature in Android. For that I created an application with an Edit text and a button. The edit text will accept text from the user and on clicking on the button will speech the text from the edit text that is entered by the user. For that first create your layout file.
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
- android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
- <TextView android:text="Please enter the text to speech" android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="20sp"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:id="@+id/textView" />
- <EditText
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/editText"
- android:layout_below="@+id/textView"
- android:layout_alignLeft="@+id/textView"
- android:layout_alignStart="@+id/textView"
- android:layout_alignRight="@+id/textView"
- android:layout_alignEnd="@+id/textView" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Speech"
- android:id="@+id/speechButton"
- android:layout_below="@+id/editText"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="43dp" />
- </RelativeLayout>
Bind the xml ui controls in to mainActivity in the onCreate(),
- editText = (EditText) findViewById(R.id.editText);
- Button button = (Button) findViewById(R.id.speechButton);
For using Text to Speech we need to initialise this and implement the onInit() method. This will contain a parameter status, please see the code below,
- textToSpeech = new TextToSpeech(MainActivity.this, new TextToSpeech.OnInitListener() {
- @Override
- public void onInit(int i) {
- if( i != TextToSpeech.ERROR){
- textToSpeech.setLanguage(Locale.US);
- }
- }
- }) ;
In the onInit() please check the status for errors, if there is no error then set your language here. There are lots of locales available, some of them are as folows
- CANADA
- CHINA
- ENGLISH
- FRANCE
- GERMANY
- ITALY
- JAPAN
- etc
Now in the speech button click call the text to speech class to convert the text in to voice. please see the code below.
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- String toSpeak = editText.getText().toString() ;
- textToSpeech.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null) ;
- }
- });
And in the onPause() we need to stop the text to speech object, please see the code below...
- public void onPause(){
- if( textToSpeech != null){
- textToSpeech.stop() ;
- textToSpeech.shutdown() ;
- }
- super.onPause();
- }
- stop() will stop the speaking.
- shutdown() release the resources used by Text To Speech engine.
Apart from the speak() there is some other methods like getLanguage(), isSpeaking() shutDown() etc.
Please see the screenshots also...