Text To Speech in Android

Text-to-speech uses many applications. It is used to speech words in your application. The text format of that speech text automatically generated in the search bar. 

In this blog, we’ll learn how to implement speech-to-text in an Android application.

Step-By-Step Implementation


Step 1: Create a new project in Android or you can use an already created project.  

Step 2: Create an XML file named Acitivity_main.xml.

Step 3: Acitivity_main.xml file continent Design of screen like button, text view, etc.

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

 

// Image view  for image load

<ImageView

android:id="@+id/idIVMic"

android:layout_width="100dp"

android:layout_height="100dp"

android:layout_centerHorizontal="true"

android:layout_marginTop="100dp"

android:src="@drawable/company_logo"

/>

 

// Textview for load text

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@id/idIVMic"

android:layout_marginTop="20dp"

android:gravity="center"

android:text="Click on Mic to speak"

android:textAlignment="center"

android:textAllCaps="false"

android:textColor="@color/black"

android:textSize="18sp" />

 

// Textview for load text

<TextView

android:id="@+id/idTVOutput"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@id/idIVMic"

android:layout_marginTop="70dp"

android:gravity="center"

android:text="Output will appear here"

android:textAlignment="center"

android:textColor="@color/black"

android:textSize="20sp" />

</RelativeLayout>  

Step 4: Create ActivityMain.kt or java in your project inside (com.my application).

import android.annotation.SuppressLint

import android.content.Intent

import android.os.Bundle

import android.speech.RecognizerIntent

import android.widget.ImageView

import android.widget.TextView

import android.widget.Toast

import androidx.activity.enableEdgeToEdge

import androidx.appcompat.app.AppCompatActivity

import androidx.core.view.ViewCompat

import androidx.core.view.WindowInsetsCompat

import java.util.Locale

import java.util.Objects

 

//created class MainAcitivty

class MainActivity : AppCompatActivity() {

lateinit var txtOutput: TextView

lateinit var txtMic: ImageView

private val REQUEST_CODE_SPEECH_INPUT = 1

@SuppressLint("MissingInflatedId")

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

txtOutput = findViewById(R.id.txtOutput)

txtMic = findViewById(R.id.txtMic)

 

//Click listener  of mic button

txtMic.setOnClickListener {

val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)

intent.putExtra(

RecognizerIntent.EXTRA_LANGUAGE_MODEL,

RecognizerIntent.LANGUAGE_MODEL_FREE_FORM

)

 

intent.putExtra(

RecognizerIntent.EXTRA_LANGUAGE,

Locale.getDefault()

)

 

intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak to text")

 

try {

startActivityForResult(intent, REQUEST_CODE_SPEECH_INPUT)

} catch (e: Exception) {

Toast.makeText(this@MainActivity, " " + e.message,Toast.LENGTH_SHORT).show()}}}

 

// catch the result  fo text to speech

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {

super.onActivityResult(requestCode, resultCode, data)

if (requestCode == REQUEST_CODE_SPEECH_INPUT) {

if (resultCode == RESULT_OK && data != null) {

val res: ArrayList<String> =

data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS) as ArrayList<String>

txtOutput.setText(

Objects.requireNonNull(res)[0])}}}}

Step 5: Output of Text to Speech.



Happy Coding!


365Bloggy May 9, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive