Saturday 16 September 2023

Speech to text code in Android Kotlin

 


Here is the code for getting text from speech on the mic.

Here is your 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"

    tools:context=".MainActivity">

    <ImageView

        android:id="@+id/idIVMic"

        android:layout_width="100dp"

        android:layout_height="100dp"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="100dp"

        android:src="@drawable/baseline_mic_24"

        app:tint="@color/black" />

    <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

        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>



And here is your MainActivity.kt


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.appcompat.app.AppCompatActivity
import java.util.*

class MainActivity : AppCompatActivity() {

    
    lateinit var outputTV: TextView
    lateinit var micIV: ImageView

   
    private val REQUEST_CODE_SPEECH_INPUT = 1

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

   
        outputTV = findViewById(R.id.idTVOutput)
        micIV = findViewById(R.id.idIVMic)

        micIV.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()
            }
        }
    }

   
    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>

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





No comments:

Post a Comment

Comments

Find Hours Diffrence in Kotlin

  In Kotlin, determining the difference in hours between two timestamps is a common task, especially in scenarios involving time-based calcu...