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 Java
MainActivity.kt
>>Watch For Java
VIDEO:
Step 1: Create a new Project or open new project
Step 2: Code
activity_main.xml<?xml version="1.0" encoding="utf-8"?><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"android:padding="5dp"tools:context=".MainActivity"><!--EditText: whose text to be spoken--><EditTextandroid:id="@+id/textEt"android:layout_width="match_parent"android:layout_height="wrap_content"android:minHeight="100dp"android:padding="10dp"android:hint="Enter text to speak..."android:gravity="start"android:background="@drawable/bg_edittext"/><!--Button: click to speak the text of EditText--><Buttonandroid:id="@+id/speakBtn"android:layout_below="@id/textEt"android:drawableLeft="@drawable/ic_speak"android:drawablePadding="5dp"style="@style/Base.Widget.AppCompat.Button.Colored"android:text="Speak"android:layout_width="wrap_content"android:layout_height="wrap_content"android:drawableStart="@drawable/ic_speak" /><!--Button: click to stop speaking text of EditText--><Buttonandroid:id="@+id/stopBtn"android:layout_below="@id/textEt"android:drawableLeft="@drawable/ic_stop"android:drawablePadding="5dp"style="@style/Base.Widget.AppCompat.Button.Colored"android:text="Stop"android:layout_alignParentEnd="true"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:drawableStart="@drawable/ic_stop" /></RelativeLayout>
MainActivity.kt
package com.blogspot.atifsoftwares.texttospeech_kotlinimport android.support.v7.app.AppCompatActivityimport android.os.Bundleimport android.speech.tts.TextToSpeechimport android.widget.Toastimport kotlinx.android.synthetic.main.activity_main.*import java.util.*class MainActivity : AppCompatActivity() {//Text To Speechlateinit var mTTS:TextToSpeechoverride fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)mTTS = TextToSpeech(applicationContext , TextToSpeech.OnInitListener { status ->if (status != TextToSpeech.ERROR){//if there is no error then set languagemTTS.language = Locale.UK}})//speak button clickspeakBtn.setOnClickListener {//get text from edit textval toSpeak = textEt.text.toString()if (toSpeak == ""){//if there is no text in edit textToast.makeText(this , "Enter text" , Toast.LENGTH_SHORT).show()}else{//if there is text in edit textToast.makeText(this , toSpeak , Toast.LENGTH_SHORT).show()mTTS.speak(toSpeak , TextToSpeech.QUEUE_FLUSH , null)}}//stop speaking button clickstopBtn.setOnClickListener {if (mTTS.isSpeaking){//if speaking then stopmTTS.stop()//mTTS.shutdown()}else{//if not speakingToast.makeText(this , "Not speaking" , Toast.LENGTH_SHORT).show()}}}override fun onPause() {if (mTTS.isSpeaking){//if speaking then stopmTTS.stop()//mTTS.shutdown()}super.onPause()}}
Step 3: Run Project
OutputText To Speech - Android Studio - Kotlin |
0 Response to "Text To Speech - Android Studio - Kotlin"