I am trying to implement speech to text in a grid view in order to execute some commands. The grid view is made of cells and I am trying to enable the speech to text on each of these cells. I have the code for this, but I wasn't able to add it in this grid view that I have, because I don't know how to connect it to the base adapter.
Here is the first part of the code:
cellgrid.adapter = cellAdapter
onClickMic()
textToSpeech = TextToSpeech(applicationContext) { i ->
if (i != TextToSpeech.ERROR) {
textToSpeech!!.language = Locale.US
}
}
Here is the second part of the code:
private fun onClickMic() {
val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
intent.putExtra(
RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
)
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.US)
startActivityForResult(intent, 10)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK && data != null) {
when (requestCode) {
10 -> {
val text = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
textCommand(text!![0])
}
}
}
}
private fun textCommand(text: String?) {
val starttv: TextView = findViewById(R.id.starttv)
when (text){
"I" -> starttv.visibility = View.GONE
}
}
I want to be able to implement this on each cell, inside the grid view, not outside of it. Any thoughts on this?