Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save StephenVinouze/0eca1e522246e3d4c4a5c39343b18c58 to your computer and use it in GitHub Desktop.
Save StephenVinouze/0eca1e522246e3d4c4a5c39343b18c58 to your computer and use it in GitHub Desktop.
private var isActivated: Boolean = false
private val activationKeyword: String = "<YOUR_KEYWORD>"
override fun onResults(results: Bundle) {
val matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)
val scores = results.getFloatArray(SpeechRecognizer.CONFIDENCE_SCORES)
if (matches != null) {
if (isActivated) {
isActivated = false
stopRecognition()
} else {
matches.firstOrNull { it.contains(other = activationKeyword, ignoreCase = true) }
?.let {
isActivated = true
}
startRecognition()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment