Android allows you convert your Text into Voice. Not only you can convert it but it also allows you to speak text in variety of different languages.
Android provides TextToSpeech class for this purpose. In order to use this class, you can implement TextToSpeech.OnInitListener this listener to your class and it will automatically add the below method to your class,
1 2 3 |
@Override public void onInit(int status) { } |
else you can specify the initListener, its syntax is given below:
1 2 3 4 5 6 7 |
private EditText edit; tts=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() { @Override public void onInit(int status) { } } ); |
In this listener, you have to specify the properties for TextToSpeech object , such as its language ,pitch e.t.c. Language can be set by calling setLanguage() method. Its syntax is given below −
1 |
int result = tts.setLanguage(Locale.US); |
The method setLanguage takes an Locale object as parameter. The list of some of the locales available are given below −
S.No. | Locale |
---|---|
1 | US |
2 | CANADA_FRENCH |
3 | GERMANY |
4 | ITALY |
5 | JAPAN |
6 | CHINA |
Once you have set the language, you can call speak method of the class to speak the text. Its syntax is given below −
1 |
tts.speak(texttospeak, TextToSpeech.QUEUE_FLUSH, null); |
Apart from the speak method, there are some other methods available in the TextToSpeech class. They are listed below:
Method | Description |
---|---|
addSpeech(String text, String filename) | This method adds a mapping between a string of text and a sound file. |
getLanguage() | This method returns a Locale instance describing the language. |
isSpeaking() | This method checks whether the TextToSpeech engine is busy speaking. |
setPitch(float pitch) | This method sets the speech pitch for the TextToSpeech engine. |
setSpeechRate(float speechRate) | This method sets the speech rate. |
shutdown() | This method releases the resources used by the TextToSpeech engine. |
stop() | This method stop the speak. |
If you want to do the other way i.e converting speech to text, follow my tutorial Android Speech to Text.
In this tutorial, we are going to learn how to convert Text to Speech.
1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. By default my activity is MainActivity.java.
2. First create a layout in xml file naming activity_main.xml that contains Button for starting TTS(TextToSpeech) and an EditText that take input and convert the text into speech.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="5dp" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="15dp" android:text="@string/text" android:textColor="#0587d9" android:textSize="26sp" android:textStyle="bold" /> <EditText android:id="@+id/speak_text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:layout_marginTop="20dp" android:hint="@string/hint" android:padding="10dp" /> <Button android:id="@+id/btnSpeak" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:padding="10dp" android:text="@string/speak" android:textSize="15sp" /> </LinearLayout> |
3. Finally come to your MainActivtiy.java and add the following code. This class contains all the code that we discussed earlier in this article.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
package com.text_to_speech; import java.util.Locale; import android.os.Bundle; import android.speech.tts.TextToSpeech; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends ActionBarActivity implements TextToSpeech.OnInitListener { private static TextToSpeech tts; private static Button btnSpeak; private static EditText speak_text; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { tts = new TextToSpeech(MainActivity.this, this); btnSpeak = (Button) findViewById(R.id.btnSpeak); speak_text = (EditText) findViewById(R.id.speak_text); // button on click event btnSpeak.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { speakOut(); } }); } @Override public void onDestroy() { // Don't forget to shutdown! if (tts != null) { tts.stop(); tts.shutdown(); } super.onDestroy(); } //Speak Out Method to speak the text private void speakOut() { String text = speak_text.getText().toString(); tts.speak(text, TextToSpeech.QUEUE_FLUSH, null); } @Override public void onInit(int status) { if (status == TextToSpeech.SUCCESS) { int result = tts.setLanguage(Locale.US);// Set Language // tts.setPitch(5); // set pitch level // tts.setSpeechRate(2); // set speech speed rate //Language is not Supported if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) { Log.e("TTS", "Language is not supported"); } else { btnSpeak.setEnabled(true); speakOut(); } } else { Log.e("TTS", "Initialization Failed"); } } } |
4. Now, you are all done, run your app and you will get the output as shown in video.
Thanks.
Subscribe to us and get the latest news.