In this video we will create a "Text To Speech" application , which can be used to speak the text from any view e.g. EditText , TextView etc.
>>Watch For Kotlin:
MainActivity.java
>>Watch For Kotlin:
VIDEO:
Step 1: Create a new project OR Open your project
Step 2: Code
activity_main.xml<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"android:padding="5dp"tools:context=".MainActivity"><!--EditText in which we will input text to speak--><EditTextandroid:id="@+id/textEt"android:layout_width="match_parent"android:layout_height="wrap_content"android:minHeight="100dp"android:gravity="start"android:background="@drawable/bg_edittext"android:padding="5dp"android:hint="Enter Text to speak..."/><!--Button: on click start reading content of EditText--><Buttonandroid:layout_below="@id/textEt"android:drawableLeft="@drawable/ic_speak"android:drawablePadding="5dp"android:id="@+id/speakBtn"android:text="Speak"style="@style/Base.Widget.AppCompat.Button.Colored"android:layout_width="wrap_content"android:layout_height="wrap_content" /><!--Stop speaking button--><Buttonandroid:layout_below="@id/textEt"android:drawableLeft="@drawable/ic_stop"android:layout_alignParentEnd="true"android:drawablePadding="5dp"android:id="@+id/stopBtn"android:text="Stop"style="@style/Base.Widget.AppCompat.Button.Colored"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true" /></RelativeLayout>
MainActivity.java
package com.blogspot.atifsoftwares.texttospeech_java;import android.speech.tts.TextToSpeech;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import java.util.Locale;public class MainActivity extends AppCompatActivity {//viewsEditText mTextEt;Button mSpeakBtn , mStopBtn;TextToSpeech mTTS;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mTextEt = findViewById(R.id.textEt);mSpeakBtn = findViewById(R.id.speakBtn);mStopBtn = findViewById(R.id.stopBtn);mTTS = new TextToSpeech(getApplicationContext() , new TextToSpeech.OnInitListener() {@Overridepublic void onInit(int status) {if (status != TextToSpeech.ERROR){//if there is no error then set languagemTTS.setLanguage(Locale.UK);}else {Toast.makeText(MainActivity.this , "Error" , Toast.LENGTH_SHORT).show();}}});//speak btn clickmSpeakBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//get text from edit textString toSpeak = mTextEt.getText().toString().trim();if (toSpeak.equals("")){//if there is no text in edit textToast.makeText(MainActivity.this , "Please enter text..." , Toast.LENGTH_SHORT).show();}else {Toast.makeText(MainActivity.this , toSpeak , Toast.LENGTH_SHORT).show();//speak the textmTTS.speak(toSpeak , TextToSpeech.QUEUE_FLUSH , null);}}});//stop btn clickmStopBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if (mTTS.isSpeaking()){//if it is speaking then stopmTTS.stop();//mTTS.shutdown();}else {//not speakingToast.makeText(MainActivity.this , "Not speaking" , Toast.LENGTH_SHORT).show();}}});}@Overrideprotected void onPause() {if (mTTS != null || mTTS.isSpeaking()){//if it is speaking then stopmTTS.stop();//mTTS.shutdown();}super.onPause();}}
Step 3: Run Project
OutputText To Speech - Android Studio - Java |
0 Response to "Text To Speech - Android Studio - Java"