How to Convert Speech To Text in Jetpack Compose

How to Convert Speech To Text in Jetpack Compose

Jetpack Compose

How to Convert Speech To Text in Jetpack Compose. Have you ever thought about how Google’s speech-search functions, or have you ever thought of creating an app for the narration of ebooks? It may appear to be a complicated technology. It isn’t easy to set up by yourself, Android (via Google Services) includes speech-to-text and text-to-speech APIs that make it easy to implement these capabilities.

How do you make Speech To Text work?

For speech-to-text, Android provides an Intent API that is based on the Google Speech Recognition service and returns the result of text back to you. There’s a catch that the device must be running Google Search app for the service to function.

The API for text-to-speech, in contrast to Speech Recognition, is available without Google Services and is available in the android.speech.tts package.

Let’s start to make jetpack compose speech to text example full source code. And Step by step guidelines speech to text google. Start your android studio and make a project with a jetpack compose activity.

Step 1:  Add Permission to the manifests file 

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>

Step 2:  Add Dependency in your module base Gradle build 

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation ''
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.material:material:$compose_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.0'
    implementation 'androidx.activity:activity-compose:1.4.0'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"

Step 3: Main Activity Kotlin Full Souce code 

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.speech.RecognizerIntent
import android.speech.SpeechRecognizer
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.jetpack.speechtotext.ui.theme.SpeechToTextTheme
import java.util.*

class MainActivity : ComponentActivity() {
    private var talk by mutableStateOf("Click to button Speech text ")

    override fun onCreate(savedInstanceState: Bundle?) {
        setContent {
            SpeechToTextTheme {
                Surface(color = MaterialTheme.colors.background) {
                        topBar = {
                                title = {
                                        text = "Speech to Text Jetpack Compose ",
                                        modifier = Modifier.fillMaxWidth(),
                                        textAlign = TextAlign.Left
                    ) {

    fun SpeechToText() {
        val context = LocalContext.current

            modifier = Modifier
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center
        ) {
                text = talk,
                style = MaterialTheme.typography.h6,
                modifier = Modifier
                textAlign = TextAlign.Center

            Spacer(modifier = Modifier.height(20.dp))

                onClick = { askSpeechInput(context = context) },
                modifier = Modifier.clip(RoundedCornerShape(10.dp))
            ) {
                    text = "Click to Speech",
                    fontSize = 20.sp,
                    modifier = Modifier
                        .padding(horizontal = 10.dp, vertical = 5.dp)

    private fun askSpeechInput(context: Context) {
        if (!SpeechRecognizer.isRecognitionAvailable(context)) {
            Toast.makeText(context, "Speech not Available", Toast.LENGTH_SHORT).show()
        } else {
            val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH)
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault())
            intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Talk Something")

            startActivityForResult(intent, 102)

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

        if (requestCode == 102 && resultCode == Activity.RESULT_OK) {
            val result = data?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
            talk = result?.get(0).toString()

After completing the all steps run your project and see the output.

Read More Tutorial