How to Convert Text to Speech in Android Studio — Text to Speech Converter using Android
Hi Guys, welcome to GBAndroidBlogs we have created a Text to Speech converter app. In this app, we convert the text into speech. Here we convert the text into the English language.
Android allows us to convert the text into a voice or speech. Also, we can convert the text into different languages like the US, JAPAN, GERMANY, ITALY, etc.
For this purpose, Android provides us a class TextToSpeech class to converts the Text into Speech.
Text to Speech Convert:
here we first initialize the TextToSpeech class then apply the OnInitListener. In the listener, we specify the properties of the TextToSpeech object, such as its language, pitch, etc.
Language is set by calling setLanguage() method, in this method we can use different language just set the parameter Locale as your language requirement. Locale is the parameter of setlanguage method.
Once we can change the language, we can call the speck method of the TextToSpeech class.
There are many methods available in the TextToSpeech class. Like
- addSpeech(String text, String filename): this method adds the mapping between the string of text and sound file.
- getLanguage(): this method returns a Locale language that is set here.
- isSpeaking(): this method checks the TextToSpeech engine is busy or not.
- setPitch(float pitch): this method sets the pitch for the TextToSpeech engine.
- setSpeedRate(float speechRate): this method sets the sound speed rate.
- shutdown(): this method frees the resources used by the TextToSpeech.
- stop(): this method stops the speak.
Here we also use the Seekbar for changing the pitch and speed rate of the speech. Then set the pitch and the speed rate.
File: main_activity.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=".MainActivity">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="50dp"
android:hint="Enter Text">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_text"
android:textColor="#000"/>
</com.google.android.material.textfield.TextInputLayout>
<LinearLayout
android:background="#C5C5C5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pitch"
android:textColor="#ffffff"
android:textSize="16sp"
android:layout_gravity="center"
android:layout_marginTop="20sp"/>
<SeekBar
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/seek_bar_pitch"
android:progress="50"
android:layout_gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Speed"
android:textSize="16sp"
android:textColor="#ffffff"
android:layout_gravity="center"/>
<SeekBar
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/seek_bar_speed"
android:progress="50"
android:layout_gravity="center"
android:layout_marginBottom="20dp"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button_speak"
android:text="Speak it"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:enabled="false"/>
</LinearLayout>
File: ActivityMain.java
package com.example.texttospeech;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import com.google.android.material.textfield.TextInputEditText;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private TextInputEditText mEditText;
private TextToSpeech mTTS;
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.ENGLISH);
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();
}
}
Output:
Also, we can go and check the video from below link-
https://drive.google.com/file/d/1DCzDSijC3zzCk6TYa7AJk7VlgQqq2m90/view?usp=sharing
And there is your Text to Speech converter app.
Quick Recap:
- Android provides a class TextToSpeech class to convert the Text into Speech.
- TextToSpeech has different methods, like setSpeedRate(), addSpeech(), setPitch(), getLanguage(), isSpeaking() etc.
- Here seek bar is used to change the pitch and speed rate.
You can also see:
If you have any query then comment below