Steps to Implement Text To Speech in Android Studio
Android allows you to convert your text into a voice. Android provides TextToSpeech class for this purpose. You need to instantiate an object of this class and also specify the initListener.
Step 1: Create a New Project
- Create a new project by going to File ⇒ New Android Project. and fill the required details.
- Implement your main Activity class from OnInitListener
- Now add the following code to your class.
Step 2: Modify MainActivity.java
Implement the main activity class from TextToSpeech.OnInitListener
to handle the initialization of the TTS engine.
package com.example.texttospeech;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.app.Activity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.view.View;
import java.util.Locale;
public class MainActivity extends Activity implements OnInitListener {
TextToSpeech t1;
EditText ed1;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1 = findViewById(R.id.editText);
b1 = findViewById(R.id.button);
t1 = new TextToSpeech(getApplicationContext(), this);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String toSpeak = ed1.getText().toString();
Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();
t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null, null);
}
});
}
@Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
t1.setLanguage(Locale.UK);
}
}
@Override
public void onPause() {
if (t1 != null) {
t1.stop();
t1.shutdown();
}
super.onPause();
}
}
Step 3: Define the Layout
Create activity_main.xml
to define the user interface with a TextView
, EditText
, and a Button
.
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">
<TextView
android:text="Text to Speech"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview"
android:textSize="35dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:hint="Enter Text"
android:layout_below="@+id/textview"
android:layout_marginTop="20dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text to Speech"
android:id="@+id/button"
android:layout_below="@+id/editText"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" />
RelativeLayout>
Step 4: Update AndroidManifest.xml
Ensure the MainActivity
is declared in your AndroidManifest.xml
.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.texttospeech">
<application
android:allowBackup="true"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
intent-filter>
activity>
application>
manifest>
Changing Language :
You can change the language to speak by using setLanguage() function. A lot of languages are supported like Canada, French, Chinese, Germany, etc.,
tts.setLanguage(Locale.CHINESE); // Chinese language
Read More: How to change the background transition in Android Studio
Changing Pitch Rate :
You can set a speed pitch level by using setPitch() function. 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.6);
Changing Speed Rate :
The speed rate can be set using setSpeechRate(). This also will take a default of 1.0 value. You can double the speed rate by setting 2.0 or make half the speed level by setting 0.5
tts.setSpeechRate(2);
Conclusion
You can easily implement text-to-speech functionality in your Android app. This feature can enhance accessibility and user interaction, making your app more user-friendly.