Android Text to Speech Tutorial

Total Views : 653
Zoom In Zoom Out Read Later Print

This tutorial explains how to work with android text to speech or android speech synthesis. In this tutorial you also learn abount changing the language type, pitch level and speed level.We will create an android application which speaks the text from EditText .

Android Text To Speech is also referred to Android TTS.

TextToSpeech as the name suggest, it is used to synthesize speech from the text string.
You can set the language of your choice for speech. You can set the pitch, speed for speech as well your own speech from a custom file.

Let's Create One Now

TextToSpeech needs to be initialized first. For this, you need to implement the TextToSpeech.OnInitListener interface and override the method: onInit.

1. First of all we need to initialize TTS.

TextToSpeech

TextToSpeech tts = new TextToSpeech(this, this);

2. Override onInit() method

onInit()

@Override
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) {
Toast.makeText(getApplicationContext(), "Language not supported", Toast.LENGTH_SHORT).show();
} else {
button.setEnabled(true);
}

} else {
Toast.makeText(getApplicationContext(), "Init failed", Toast.LENGTH_SHORT).show();
}
}

3. You need to use speek() method to speek text.

speak()

tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, null);

As you can see Speak method accept four paramaters. First one for text, second parameter defines that previous input is flushed so as to begin a clean slate. Third parameter is for bundle that is passed and last one for used to listen for the tts events.

4. You can change language to speak by using setLanguage() function of TTS.

setlanguage()

tts.setLanguage(Locale.US);

5. You can set speed pitch level by using setPitch() function. Default pitch level value is 1.0.

setPitch()

tts.setPitch(0.8);

6. The speed can be set using setSpeechRate(). default value is 1.0.

setSpeechRate()

tts.setSpeechRate(1.5);


Ok, No all in one.

MainActivity.java

package com.prodevsblog.tts;

import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Locale;

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
private TextToSpeech tts;
Button button, btnChangePitch, btnChangeSpeed;
EditText editText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
void init(){
tts = new TextToSpeech(this, this);

button = findViewById(R.id.button);
btnChangePitch = findViewById(R.id.btnChangePitch);
btnChangeSpeed = findViewById(R.id.btnChangeSpeed);
editText = findViewById(R.id.enterYouText);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
speakOut();
}
});
btnChangePitch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tts.setPitch(0.2f);
}
});
btnChangeSpeed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tts.setSpeechRate(2f);
}
});
}
@Override
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) {
Toast.makeText(getApplicationContext(), "Language not supported", Toast.LENGTH_SHORT).show();
} else {
button.setEnabled(true);
}
} else {
Toast.makeText(getApplicationContext(), "Init failed", Toast.LENGTH_SHORT).show();
}
}

private void speakOut() {
tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onStart(String s) {
final String s2 = s;
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Started" + s2, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onDone(String s) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Done ", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onError(String s) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Error ", Toast.LENGTH_SHORT).show();
}
});
}
});

Bundle params = new Bundle();
params.putString(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, ""); // Define if you need it
String text = editText.getText().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, params, "Aveee");
}

@Override
public void onDestroy() {
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical">
<EditText
android:id="@+id/enterYouText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:text="ProDevsBlog.com" />


<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="SPEAK TEXT" />

<Button
android:id="@+id/btnChangePitch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Change Pitch"
/>

<Button
android:id="@+id/btnChangeSpeed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Change Speed" />
</LinearLayout>
</RelativeLayout>

See More

Latest Photos