Text To Speech | Android Studio | Compose

How to implement Text to Speech using Android Studio Jetpack Compose?

In this tutorial, we will create a "Text To Speech" application that can be used to speak text from any view, such as an EditText or TextView.


Code

MainActivity.kt

package com.technifysoft.myapplication

import android.os.Bundle
import android.speech.tts.TextToSpeech
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Button
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
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.input.ImeAction
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.technifysoft.myapplication.ui.theme.MyApplicationTheme
import java.util.Locale

/**
 * 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

    var inputText by remember { mutableStateOf("") }
    var textToSpeech: TextToSpeech? by remember { mutableStateOf(null) }

    // Initialize TTS
    DisposableEffect(Unit) {
        textToSpeech = TextToSpeech(context) { status ->
            if (status == TextToSpeech.SUCCESS) {
                // Ready
            } else {
                Toast.makeText(context, "Text To Speech error", Toast.LENGTH_SHORT).show()
            }
        }

        onDispose {
            textToSpeech?.stop()
            textToSpeech?.shutdown()
        }
    }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(20.dp),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Text(modifier = Modifier.padding(top = 40.dp), text = "Text To Speech")

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

        TextField(
            value = inputText,
            onValueChange = { inputText = it },
            modifier = Modifier
                .fillMaxWidth()
                .heightIn(min = 100.dp),
            placeholder = { Text("Enter text to speak...") },
            singleLine = false,
            keyboardOptions = KeyboardOptions.Default.copy(imeAction = ImeAction.Done)
        )

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

        Row(modifier = Modifier.fillMaxWidth()) {
            Button(onClick = {
                val toSpeak = inputText.trim()
                if (toSpeak.isEmpty()) {
                    Toast.makeText(context, "Please enter text...", Toast.LENGTH_SHORT).show()
                } else {
                    val locale = Locale.forLanguageTag("en")
                    val result = textToSpeech?.setLanguage(locale)
                    if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        Toast.makeText(context, "Language Not supported", Toast.LENGTH_SHORT).show()
                    } else {
                        textToSpeech?.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null, null)
                    }
                }
            }) {
                Text("Speak")
            }

            Spacer(modifier = Modifier.weight(1f))

            Button(onClick = {
                if (textToSpeech?.isSpeaking == true) {
                    textToSpeech?.stop()
                } else {
                    Toast.makeText(context, "Not Speaking", Toast.LENGTH_SHORT).show()
                }
            }) {
                Text("Stop")
            }
        }

    }
}

/**
 * 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())
    }
}

Screenshots:

Text To Speech | Android Studio | ComposeText To Speech | 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