Speech To Text - Android Studio - Compose

Speech To Text

In this comprehensive tutorial, you'll learn how to implement Speech to Text functionality in an Android application using Kotlin (Jetpack Compose) 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.

Code:

MaintActivity.kt

package com.technifysoft.myapplication

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.speech.RecognizerIntent
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.compose.setContent
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.technifysoft.myapplication.ui.theme.MyApplicationTheme

/**
 * MainActivityCompose is the main activity of the application, responsible for setting up the UI.
 */
class MainActivityCompose : ComponentActivity() {
    /**
     * Called when the activity is first created. This is where you should do all of your normal static set up:
     * create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's
     * previously frozen state, if there was one.
     */
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyApplicationTheme {

                Scaffold(
                    modifier = Modifier.fillMaxSize(),
                ) { innerPadding ->
                    MainUI(innerPadding)
                }

            }
        }
    }
}

@Composable
fun MainUI(innerPadding: PaddingValues) {
    // Get the current context
    val context = LocalContext.current

    // State to hold the spoken text
    var spokenText by remember { mutableStateOf("") }

    // ActivityResultLauncher for handling the result of the voice input activity
    val voiceInputArl = rememberLauncherForActivityResult(
        contract = ActivityResultContracts.StartActivityForResult()
    ) { activityResult ->
        // Check if the result is OK
        if (activityResult.resultCode == Activity.RESULT_OK) {
            // Get the recognized speech results
            val result = activityResult.data?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
            // Append the first result to the spokenText state
            result?.get(0)?.let { recognizedText ->
                spokenText += recognizedText
            }
        }
    }

    /**
     * Function to initiate voice input.
     *
     */
    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).apply {
            putExtra(
                RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
            )
            putExtra(RecognizerIntent.EXTRA_LANGUAGE, language)
            putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak to text")
        }

        // Launch the voice input activity
        try {
            voiceInputArl.launch(intent)
        } catch (e: Exception) {
            // Show a toast message if an error occurs
            Toast.makeText(context, " ${e.message}", Toast.LENGTH_SHORT).show()
        }
    }

    // Column layout to arrange UI elements vertically
    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(innerPadding)
            .padding(16.dp),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center
    ) {
        // Text displaying "Speech To Text:"
        Text(text = "Speech To Text:", style = TextStyle(fontSize = 20.sp))
        // Button to trigger voice input
        Button(
            modifier = Modifier.padding(vertical = 20.dp),
            onClick = { voiceInput() }) {
            Text("Voice Input")
        }
        // Text displaying the spoken text
        Text(text = spokenText)
    }
}

/**
 * GreetingPreview is a composable function for previewing the MainUI in Android Studio.
 * It is annotated with @Preview to enable live preview.
 *
 */
@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    MyApplicationTheme {
        MainUI(PaddingValues())
    }
}

Screenshot
Speech To Text - Android Studio - Compose



Comments

Popular posts from this blog

Picture In Picture | Android Studio | Kotlin

Manage External Storage Permission | Android Studio | Kotlin

How to add AIDL folder | Android Studio