Android Text to Speech Tutorial
|
|
In this tutorial we will learn about text to speech (TTS) or speech synthesis. Text to speech is quite useful feature which speaks the text in different languages. We can implement text to speech functionality in our own application using TextToSpeech API. The TextToSpeech API is built in to Android (Android 1.6 or later).
Text to Speech Example
Please consider a simple example app with one input field and a button to
trigger a event that will take text from input field and speaks out.
1. Create a new project by going to File ⇒ New Android Project and fill required details:
Project Name : Text2Speech
Package : com.sunil.text2speech
Main Activity : Main.java
Layout : main.xml
Build Target : Android 2.2
2. Open the res/layout/main.xml file and insert the following code:
<LinearLayout 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:background="#777"
android:orientation="vertical" >
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="15dip"
android:background="#777"
android:text="TEXT TO SPEECH" />
<EditText
android:id="@+id/txtText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_marginTop="20dip" />
<Button
android:id="@+id/btnSpeak"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="15dip"
android:text="Speak" />
</LinearLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#777"
android:orientation="vertical" >
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="15dip"
android:background="#777"
android:text="TEXT TO SPEECH" />
<EditText
android:id="@+id/txtText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_marginTop="20dip" />
<Button
android:id="@+id/btnSpeak"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="15dip"
android:text="Speak" />
</LinearLayout>
3. Now open the Main.java file and add the following code:
package com.sunil.text2speech;
import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Main extends Activity implements
TextToSpeech.OnInitListener {
/** Called when the activity is first created. */
private TextToSpeech tts;
private Button btnSpeak;
private EditText txtText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tts = new TextToSpeech(this, this);
btnSpeak = (Button) findViewById(R.id.btnSpeak);
txtText = (EditText) findViewById(R.id.txtText);
// button on click event
btnSpeak.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
speakOut();
}
});
}
@Override
public void onDestroy() {
// Don't forget to shutdown tts!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
btnSpeak.setEnabled(true);
speakOut();
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
private void speakOut() {
String text = txtText.getText().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Main extends Activity implements
TextToSpeech.OnInitListener {
/** Called when the activity is first created. */
private TextToSpeech tts;
private Button btnSpeak;
private EditText txtText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tts = new TextToSpeech(this, this);
btnSpeak = (Button) findViewById(R.id.btnSpeak);
txtText = (EditText) findViewById(R.id.txtText);
// button on click event
btnSpeak.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
speakOut();
}
});
}
@Override
public void onDestroy() {
// Don't forget to shutdown tts!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
btnSpeak.setEnabled(true);
speakOut();
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
private void speakOut() {
String text = txtText.getText().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
4. Now run and test your project by entering some text in input field.
Note:- To get started with TTS, first we have to create a TextToSpeech object. After that we have to perform a couple of tasks, one to check that the TTS data are available and/or install them if not ( refer to code inside onInit() ) and another to start the TTS mechanism ( refer to code inside speakOut() ) and finally we have to stop/shutdown TTS ( refer to code inside onDestroy() ).
Useful methods related to TTS
There are three useful methods related to TTS: setLanguage(), setPitch(), setSpeechRate().
setLanguage() :- This method is used to set a particular language for TTS. Lot of languages are supported like Canada, French, Chinese, Germany etc.
tts.setLanguage(Locale.CHINESE); // Chinese language
setPitch() :- This method is used to set speed pitch level. By default the value is 1.0, You can set lower values than 1.0 to decrease pitch level or greater values for increase pitch level.
tts.setPitch(0.7);
setSpeechRate() :- This method is used to set speech rate. The default speech rate is 1.0. You can double the speed rate by setting 2.0 or make half the speed level by setting 0.5.
tts.setSpeechRate(2);
That's it ! You can also download the source code of this tutorial.










Post a Comment