Full source code to use the text to speech API in Android. We will set up ab OnInitListener, override the onInit method, set our desired language and take care of any Exceptions. We will also adjust the pitch and speed of the spoken text using SeekBars
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context="tech.codingpoint.texttospeechexample.MainActivity">
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:hint="Enter Text" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pitch"
android:textSize="16sp" />
<SeekBar
android:id="@+id/seek_bar_pitch"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:progress="50" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Speed"
android:textSize="16sp" />
<SeekBar
android:id="@+id/seek_bar_speed"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:progress="50" />
<Button
android:id="@+id/button_speak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:enabled="false"
android:text="Say it!" />
</LinearLayout>
MainActivity.java
package tech.codingpoint.texttospeechexample;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private TextToSpeech mTTS;
private EditText mEditText;
private SeekBar mSeekBarPitch;
private SeekBar mSeekBarSpeed;
private Button mButtonSpeak;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButtonSpeak = findViewById(R.id.button_speak);
mTTS = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = mTTS.setLanguage(Locale.GERMAN);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "Language not supported");
} else {
mButtonSpeak.setEnabled(true);
}
} else {
Log.e("TTS", "Initialization failed");
}
}
});
mEditText = findViewById(R.id.edit_text);
mSeekBarPitch = findViewById(R.id.seek_bar_pitch);
mSeekBarSpeed = findViewById(R.id.seek_bar_speed);
mButtonSpeak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
speak();
}
});
}
private void speak() {
String text = mEditText.getText().toString();
float pitch = (float) mSeekBarPitch.getProgress() / 50;
if (pitch < 0.1) pitch = 0.1f;
float speed = (float) mSeekBarSpeed.getProgress() / 50;
if (speed < 0.1) speed = 0.1f;
mTTS.setPitch(pitch);
mTTS.setSpeechRate(speed);
mTTS.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
@Override
protected void onDestroy() {
if (mTTS != null) {
mTTS.stop();
mTTS.shutdown();
}
super.onDestroy();
}
}