Kotlin Speech Recognition Without Google Api or any pop ups

18 views Asked by At

I'm working on an Android app where I need to implement speech recognition without using the Google API or any popups. Currently, when I click the mic button, it triggers the Google Speech Recognition popup, but I want the speech recognition to run in the background without any popups or reliance on Google services.

Here's my code in Speech Recognition:

// Code for initializing the speech recognizer and handling user input
var lcode = "en-US"
var languages = arrayOf<String?>("English", "Tagalog")
var lcodes = arrayOf("en-US", "fil-PH")

micbtn?.setOnClickListener(View.OnClickListener {
    val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
    intent.putExtra(
        RecognizerIntent.EXTRA_LANGUAGE_MODEL,
        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
    )
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, lcode)
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak now!")
    
    activityResultLauncher.launch(intent)
})

var activityResultLauncher = registerForActivityResult<Intent, ActivityResult>(
    ActivityResultContracts.StartActivityForResult()
) { result ->
    if (result.resultCode == RESULT_OK && result.data != null) {
        val d = result.data!!.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
        if (edtext?.text?.isNotEmpty() == true) {
            edtext?.append(" ")
        }
        edtext?.append(d?.get(0).toString())
    }
}

override fun onItemSelected(adapterView: AdapterView<*>?, view: View, i: Int, l: Long) {
    lcode = lcodes[i]
}

override fun onNothingSelected(adapterView: AdapterView<*>?) {
    // No action needed for this case
}

Could someone please guide me on how to modify this code so that the speech recognition runs in the background without any popups or reliance on Google services? Thank you.

0

There are 0 answers