<p class="p1">Android is providing a cool feature (from Android 1.6) called <span style="color: #0000ff;"><strong>Text to Speech (TTS) or speech synthesis</strong></span> which speaks the text in different languages.</p>



<p>This tutorial is about how to work with android text to speech or android speech synthesis. It also includes changing the language type, speech pitch and speech rate.</p>



<div class="wp-block-buttons aligncenter is-layout-flex wp-block-buttons-is-layout-flex">
<div class="wp-block-button is-style-fill"><a class="wp-block-button__link has-white-color has-text-color has-background" href="https://github.com/arunk7839/TextToSpeechDemo" style="background-color:#520599" target="_blank" rel="noreferrer noopener"><strong>DOWNLOAD CODE</strong></a></div>
</div>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<amp-youtube layout="responsive" width="1200" height="675" data-videoid="OEiHmipXEgw" title="Android TextToSpeech Example"><a placeholder href="https://youtu.be/OEiHmipXEgw"><img src="https://i.ytimg.com/vi/OEiHmipXEgw/hqdefault.jpg" layout="fill" object-fit="cover" alt="Android TextToSpeech Example"></a></amp-youtube>
</div></figure>



<h3 class="wp-block-heading"><span style="color: #000080;"><strong>Creating new Project</strong></span></h3>



<p class="p1"><b>1</b>. Create a new project by going to ;<span style="color: #008000;"><b>File </b><span class="s1"><b>⇒</b></span></span><b><span style="color: #008000;"> New Android Project</span>,</b> fill the required details and then click on <span style="color: #0000ff;"><strong>finish</strong></span>.</p>



<p class="p1"><b>2</b>. The below layout file consist of a textView , an input field and a button to trigger a event that will take text from input field and speaks out.</p>



<p><strong><span style="color: #0000ff;">activity_main.xml</span></strong></p>



<pre class="wp-block-preformatted"><;?xml version="1.0" encoding="utf-8"?>;
<;androidx.constraintlayout.widget.ConstraintLayout 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"
 tools:context=".MainActivity">;

 <;TextView
 android:id="@+id/tv_text_to_speech"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_marginTop="@dimen/margin_50dp"
 android:gravity="center"
 android:text="@string/text_to_speech"
 android:textColor="@color/colorPrimaryDark"
 android:textSize="@dimen/textSize_30sp"
 android:textStyle="bold"
 app:layout_constraintEnd_toEndOf="parent"
 app:layout_constraintStart_toStartOf="parent"
 app:layout_constraintTop_toTopOf="parent" />;

 <;EditText
 android:id="@+id/edt_text"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_marginStart="@dimen/margin_10dp"
 android:layout_marginEnd="@dimen/margin_10dp"
 android:layout_marginTop="@dimen/margin_30dp"
 android:ems="10"
 android:background="@drawable/background"
 android:hint="@string/enter_some_text_to_speak"
 android:padding="@dimen/padding_12dp"
 app:layout_constraintEnd_toEndOf="@+id/tv_text_to_speech"
 app:layout_constraintHorizontal_bias="0.0"
 app:layout_constraintStart_toStartOf="@+id/tv_text_to_speech"
 app:layout_constraintTop_toBottomOf="@+id/tv_text_to_speech" />;

 <;Button
 android:id="@+id/btn_speak"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginTop="@dimen/margin_30dp"
 android:background="@color/colorPrimary"
 android:text="@string/speak_text"
 android:padding="@dimen/padding_12dp"
 android:textStyle="bold"
 android:textSize="@dimen/textSize_18sp"
 android:textColor="@android:color/white"
 app:layout_constraintEnd_toEndOf="@+id/edt_text"
 app:layout_constraintStart_toStartOf="@+id/edt_text"
 app:layout_constraintTop_toBottomOf="@+id/edt_text" />;

<;/androidx.constraintlayout.widget.ConstraintLayout>;</pre>



<p><div class="article-post-image"><strong>3</strong>. Open <span style="color: #008000;"><strong><span style="color: #008000;">MainActivity.java</span><span style="color: #000000;">,</span></strong></span> implement it from <span style="color: #0000ff;"><strong>TextToSpeech.OnInitListener </strong></span>and add the below code<strong>.</strong></div></p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>




<p><span style="color: #0000ff;"><strong>MainActivity.java</strong></span></p>



<pre class="wp-block-preformatted">package com.c1ctech.texttospeechdemo;

import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.Locale;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {

 EditText edtText;
 Button btnSpeak;
 TextToSpeech tts;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 <strong><span style="color: #008000;">//get views by id</span></strong>
 edtText = findViewById(R.id.edt_text);
 btnSpeak = findViewById(R.id.btn_speak);

 tts = new TextToSpeech(this, this);

 <strong><span style="color: #008000;">// button on click event</span></strong>
 btnSpeak.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View view) {

 <strong><span style="color: #008000;"> //get edt value</span></strong>
 String text = edtText.getText().toString();

 tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, "abc");
 }
 });

 }

 @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) {
 Log.e("TTS", "This Language is not supported");
 }
 } else {
 Log.e("TTS", "Initilization Failed!");
 }

 }

 @Override
 public void onDestroy() {
 <strong><span style="color: #008000;">//don't forget to shutdown tts</span></strong>
 if (tts != null) {
 tts.stop();
 tts.shutdown();
 }
 super.onDestroy();
 }

}</pre>



<p>In the above code,</p>



<p><span style="color: #0000ff;"><strong>onInit()</strong></span>: Called to signal the completion of the TextToSpeech engine initialization. Here, you have to specify the properties for TextToSpeech object , such as its language ,pitch e.t.c.</p>



<p class="p1"><strong><span style="color: #0000ff;">speak()</span></strong>: Speaks the text using the specified queuing strategy and speech parameters.</p>



<p class="p1"><strong><span style="color: #0000ff;">setLanguage()</span></strong>: Sets the text-to-speech language.</p>



<p class="p1"><strong><span style="color: #0000ff;">stop()</span></strong>: stop the speak.</p>



<p class="p1"><strong><span style="color: #0000ff;">shutdown()</span></strong>: releases the resources used by the TextToSpeech engine.</p>



<p><strong>4</strong>. Now run your project and test your app by entering some text in input filed.</p>



<div class="wp-block-image"><figure class="aligncenter is-resized"><img src="https://c1ctech.com/wp-content/uploads/2021/01/Screenshot_1611838103-576x1024.png" alt="" class="wp-image-2346" width="381" height="678"/></figure></div>



<h4 class="wp-block-heading"> ;</h4>



<h4 class="p1 wp-block-heading"><strong><span style="color: #000080;">Changing Language</span></strong></h4>



<p class="p1">You can change language to speak by using ;<span style="color: #0000ff;"><b>setLanguage()</b></span> method. Lot of languages are supported like Canada, French, Chinese, Germany etc.,</p>



<pre class="wp-block-preformatted">tts.setLanguage(Locale.CHINESE); <strong><span style="color: #008000;">// Chinese language</span></strong>
tts.setLanguage(Locale.FRENCH); <span style="color: #008000;"><strong>// French language</strong></span>
tts.setLanguage(Locale.GERMAN); <strong><span style="color: #008000;">// German language</span></strong></pre>



<h4 class="p1 wp-block-heading"><strong><span style="color: #000080;">Changing Pitch Rate</span></strong></h4>



<p class="p1">You can set speech pitch for the TextToSpeech engine by using <span style="color: #0000ff;"><b>setPitch()</b> ;</span>function. By default the value is ;<b>1.0</b> ;You can set lower values than ;<b>1.0</b> ;to decrease pitch level or greater values for increase pitch level.</p>



<pre class="wp-block-preformatted">tts.setPitch(0.6f);</pre>



<h4 class="p1 wp-block-heading"><strong><span style="color: #000080;">Changing Speech Rate</span></strong></h4>



<p class="p1">The speech rate can be set using <span style="color: #0000ff;"><b>setSpeechRate()</b></span>. It also takes default of ;<b>1.0</b> value. </p>



<pre class="wp-block-preformatted">tts.setSpeechRate(2f);</pre>


