In this article we will read How TextToSpeech works in Android. In Android you can convert text to speech by the help of TextToSpeech class. If you want to use this class you need to instantiate Object of this class and you need to implement TextToSpeech.OnInitListener interface.
Constructor of TextToSpeech class
1 |
TextToSpeech(Context context,TextToSpeech.OnInitListener) |
Let us start code for this.
activity_main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?xml version="1.0" encoding="utf-8"?> <androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" 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:textSize="35sp" android:layout_gravity="center_horizontal"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/editText" android:layout_marginTop="35dp" android:hint="Enter Text" android:textColor="#ff0000" android:textColorHint="#0023d1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Text to Speech" android:id="@+id/button" android:layout_gravity="center_horizontal" android:layout_marginTop="35dp" /> </androidx.appcompat.widget.LinearLayoutCompat> |
In activity_main.xml we take an edittext and a button.
MainActivity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
public class MainActivity extends AppCompatActivity { 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(), 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(); } } |
In TextToSpeech.OnInitListener you have to need properties such as langguages.Language can be set by calling as setLanguage().
1 2 |
t1.setLanguage(Locale.UK); |
Once you have set languege and then you can call speak method.
1 2 |
t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null); |
shutdown()
And by the help of this method you can released the resources used by the TextToSpeech.
stop()
this method used to stop speak.
while you enter text in editeText and then you click on button Text To Speech.
Then your text will be converted to your speech and you can listen voice in real device.
Please check below screenshots we have attached two screenshots. When you enter text to edittext then you will hear voice after clicking on text to speech button.
So Here we discussed How TextToSpeech works in Android. Thanks for reading this blog and You can get other blogs from here