Text to speech(TTS)-Android
I am new to the android platform. Now I am working on TTS(Text to Speech)
.If I enter the text in a TextArea and I would like it to be converted to speech when i click the speak button.
Can anyone help me out?
Solution 1:
Text to speech is built into Android 1.6+. Here is a simple example of how to do it.
TextToSpeech tts = new TextToSpeech(this, this);
tts.setLanguage(Locale.US);
tts.speak("Text to say aloud", TextToSpeech.QUEUE_ADD, null);
More info: http://android-developers.blogspot.com/2009/09/introduction-to-text-to-speech-in.html
Here are instructions on how to download sample code from the Android SDK Manager:
-
Launch the Android SDK Manager.
a. On Windows, double-click the SDK Manager.exe file at the root of the Android SDK directory.
b. On Mac or Linux, open a terminal to the tools/ directory in the Android SDK, then execute android sdk.
Expand the list of packages for the latest Android platform.
- Select and download Samples for SDK. When the download is complete, you can find the source code for all samples at this location:
/sdk/samples/android-version/
(i.e. \android-sdk-windows\samples\android-16\ApiDemos\src\com\example\android\apis\app\TextToSpeechActivity.java)
Solution 2:
MainActivity.class
import java.util.Locale;
import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences.Editor;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
String text;
EditText et;
TextToSpeech tts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et=(EditText)findViewById(R.id.editText1);
tts=new TextToSpeech(MainActivity.this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if(status == TextToSpeech.SUCCESS){
int result=tts.setLanguage(Locale.US);
if(result==TextToSpeech.LANG_MISSING_DATA ||
result==TextToSpeech.LANG_NOT_SUPPORTED){
Log.e("error", "This Language is not supported");
}
else{
ConvertTextToSpeech();
}
}
else
Log.e("error", "Initilization Failed!");
}
});
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
if(tts != null){
tts.stop();
tts.shutdown();
}
super.onPause();
}
public void onClick(View v){
ConvertTextToSpeech();
}
private void ConvertTextToSpeech() {
// TODO Auto-generated method stub
text = et.getText().toString();
if(text==null||"".equals(text))
{
text = "Content not available";
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}else
tts.speak(text+"is saved", TextToSpeech.QUEUE_FLUSH, null);
}
}
activity_main.xml
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="177dp"
android:onClick="onClick"
android:text="Button" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="81dp"
android:ems="10" >
<requestFocus />
</EditText>
</RelativeLayout>
Solution 3:
A minimalistic example to quickly test the TTS system:
private TextToSpeech textToSpeechSystem;
@Override
protected void onStart() {
super.onStart();
textToSpeechSystem = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
String textToSay = "Hello world, this is a test message!";
textToSpeechSystem.speak(textToSay, TextToSpeech.QUEUE_ADD, null);
}
}
});
}
If you don't use localized messages textToSpeechSystem.setLanguage(..)
is important as well, since your users probably don't all have English set as their default language so the pronunciation of the words will be wrong. But for testing TTS in general this snippet is enough
Related links: https://developer.android.com/reference/android/speech/tts/TextToSpeech