Skip to content

Instantly share code, notes, and snippets.

@bookuha
Created May 4, 2023 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bookuha/4790ec2a8ee97ab048d0a575bdf2c3e8 to your computer and use it in GitHub Desktop.
Save bookuha/4790ec2a8ee97ab048d0a575bdf2c3e8 to your computer and use it in GitHub Desktop.
package com.example.kpiz
import android.app.Activity
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.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Mic
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.*
import com.example.kpiz.ui.theme.KPIZTheme
import com.example.kpiz.ui.theme.greenColor
import java.util.*
class MainActivity : ComponentActivity() {
var outputTxt by mutableStateOf("Click button for Speech text ")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
KPIZTheme {
Surface(color = MaterialTheme.colors.background) {
Scaffold(
topBar = {
TopAppBar(
title = {
Text(
text = "Speech to Text",
modifier = Modifier.fillMaxWidth(),
textAlign = TextAlign.Center
)
})
}) {it
Column(modifier = Modifier.padding(4.dp)) {
SpeechToText()
}
}
}
}
}
}
@Composable
fun SpeechToText() {
val context = LocalContext.current
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Text(
text = "Speech to Text Example",
style = MaterialTheme.typography.h6,
modifier = Modifier
.fillMaxWidth()
.padding(15.dp),
textAlign = TextAlign.Center
)
Spacer(modifier = Modifier.height(30.dp))
Button(
elevation = ButtonDefaults.elevation(
defaultElevation = 0.dp, pressedElevation = 0.dp, disabledElevation = 0.dp
),
colors = ButtonDefaults.buttonColors(backgroundColor = Color.Transparent),
onClick = { getSpeechInput(context = context) },
) {
Icon(
imageVector = Icons.Filled.Mic,
contentDescription = "Mic",
tint = greenColor,
modifier = Modifier
.height(100.dp)
.width(100.dp)
.padding(5.dp)
)
}
Spacer(modifier = Modifier.height(30.dp))
Text(
text = outputTxt,
style = MaterialTheme.typography.h6,
textAlign = TextAlign.Center,
modifier = Modifier
.fillMaxWidth()
.padding(16.dp),
)
}
}
private fun getSpeechInput(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, "Speak Something")
startActivityForResult(intent, 101)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 101 && resultCode == Activity.RESULT_OK) {
val result = data?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
outputTxt = result?.get(0).toString()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment