How to change text to speech voice in Android

Hello Guys , 

   In this tutorial , we are going to create convert text to speech voice app on android studio. In this app convert the text into speech like you have written "Hello" in the editext and whenever you press the button it will speek "Hello".  This app is very useful for people who have trouble to read text on the screen. Here we create one edittext and  button. edittext is useful for wriiten text and button is used to convert text into speech.


TextToSpeech App MainScreen looking like below picture:




Step 1 : Create a new Project

Step 2 : Edit activity_main.xml file

  • In this xml file we create one edittext and one button under Relative Layout.
  • EditText is used to written text. Button is used to convert text into speak.
<RelativeLayout
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">

<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter text"
android:textColorHint="@color/cardview_dark_background"
android:layout_centerInParent="true"
android:layout_margin="20dp"/>


<Button
android:id="@+id/btn_speech"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Convert text into Speech"
android:textAllCaps="false"
android:background="@color/design_default_color_secondary"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:layout_marginBottom="140dp"/>

</RelativeLayout>


Step 3 : Implement MainActivity.java class file.

  • TextToSpeech class is used to convert text into speech for better user interface.
import androidx.appcompat.app.AppCompatActivity;

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

import java.util.Locale;

public class MainActivity extends AppCompatActivity {

EditText text;
Button speech;
TextToSpeech textToSpeech;


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

text = findViewById(R.id.edit_text);
speech = findViewById(R.id.btn_speech);



speech.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

textToSpeech = new TextToSpeech(getApplicationContext(),
new TextToSpeech.OnInitListener() {
@Override
public void onInit(int i) {

if(i == TextToSpeech.SUCCESS){

textToSpeech.setLanguage(Locale.getDefault());
textToSpeech.setSpeechRate(1.0f);
textToSpeech.speak(text.getText().toString(),
TextToSpeech.QUEUE_FLUSH , null);
}

}
});


}
});
}

}

  • Implement the TextToSpeech.OnInitListner() method to be notified the completion of initialization.
  • setLanguage() method is used to set the language for text-to-speech.
  • setSpeechRate() method is used to set the speech rate.
  • speak() method is used to speak the text using specified parameters.
  • QUEUE_FLUSH is used to replace old entry by the new entry.
  

Output :

If you liked this post do comment ,share and promote the post 🙏 . Please stay with us and support.  🙏  

Post a Comment

0 Comments