Text-to-speech (TTS) technology reads clearly digital manual – the words on computers, smartphones, and tablets. This tutorial explains how to implement text to speech in android or android speech synthesis.

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.

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.

MainActivity.class

public class MainActivity extends Activity { TextToSpeech t1; EditText ed1; Button b1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ed1=(EditText)findViewById(R.id.editText); b1=(Button)findViewById(R.id.button); t1=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() { @Override public void onInit(int status) { if(status != TextToSpeech.ERROR) { t1.setLanguage(Locale.UK); } } }); 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); } }); } public void onPause(){ if(t1 !=null){ t1.stop(); t1.shutdown(); } super.onPause(); } } Here is the content of 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" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:transitionGroup="true"> <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" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tutorials point" android:id="@+id/textView" android:layout_below="@+id/textview" android:layout_centerHorizontal="true" android:textColor="#ff7aff24" android:textSize="35dp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:src="@drawable/abc" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" android:theme="@style/Base.TextAppearance.AppCompat" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText" android:layout_below="@+id/imageView" android:layout_marginTop="46dp" android:hint="Enter Text" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:textColor="#ff7aff10" android:textColorHint="#ffff23d1" /> <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="46dp" /> </RelativeLayout>

Here is the content of AndroidManifest.xml

<activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" > <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>

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);