Speech To Text | Android Studio | Kotlin

Speech To Text

In this comprehensive tutorial, you'll learn how to implement Speech to Text functionality in an Android application using Java programming language in Android Studio. The tutorial covers step-by-step instructions, starting from setting up the project in Android Studio, integrating the necessary permissions, implementing the SpeechRecognizer API, handling runtime permissions, and displaying the recognized text on the user interface. Whether you're a beginner or an experienced Android developer, this tutorial will provide you with the necessary guidance to seamlessly integrate Speech to Text functionality into your Android application.

>>Check For Java

Code:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="20dp"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Speech To Text"
        android:textSize="20sp" />

    <!--Button: on click start taking voice input-->
    <com.google.android.material.button.MaterialButton
        android:id="@+id/speakBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginVertical="20dp"
        android:drawablePadding="5dp"
        android:text="Voice Input" />

    <!--TextView: set the spoken text-->
    <TextView
        android:id="@+id/spokenTextTv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="" />

</LinearLayout>

MainActivity.kt

package com.technifysoft.myapplication

import android.content.Intent
import android.os.Bundle
import android.speech.RecognizerIntent
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.button.MaterialButton

class Main1Activity : AppCompatActivity() {

    private lateinit var speakBtn: MaterialButton
    private lateinit var spokenTextTv: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        speakBtn = findViewById(R.id.speakBtn)
        spokenTextTv = findViewById(R.id.spokenTextTv)

        speakBtn.setOnClickListener { view: View? ->
            //voice input
            voiceInput()
        }
    }

    private fun voiceInput() {
        //language e.g. "en" for "English", "ur" for "Urdu", "hi" for "Hindi" etc.
        val language = "en"

        val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language)
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak to text")

        try {
            voiceInputArl.launch(intent)
        } catch (e: Exception) {
            Toast.makeText(this, " " + e.message, Toast.LENGTH_SHORT).show()
        }
    }

    private val voiceInputArl = registerForActivityResult(
        ActivityResultContracts.StartActivityForResult()
    ) { activityResult ->
        if (activityResult.resultCode == RESULT_OK) {
            val result = activityResult.data!!.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)

            val text = result!![0]

            spokenTextTv.append(text)
        }
    }
}

Output:



Comments

Popular posts from this blog

Manage External Storage Permission | Android Studio | Kotlin

Manage External Storage Permission | Android Studio | Java

Add a Back Button to Action Bar Android Studio (Kotlin)