Skip to content

Instantly share code, notes, and snippets.

@anshajkhare
Created April 20, 2021 14:41
Show Gist options
  • Select an option

  • Save anshajkhare/f774db2485d8aabb4a618879accac4e1 to your computer and use it in GitHub Desktop.

Select an option

Save anshajkhare/f774db2485d8aabb4a618879accac4e1 to your computer and use it in GitHub Desktop.
Handle audio sessions when user clicks on trigger icon
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
// ...
trigger.setOnClickListener {
// Handle audio sessions here
if (mIsListening) {
handleSpeechEnd()
} else {
handleSpeechBegin()
}
}
// ...
}
private fun handleSpeechBegin() {
// start audio session
mUserInfoText!!.setText(R.string.listening)
mIsListening = true
mSpeechRecognizer!!.startListening(createIntent())
}
private fun handleSpeechEnd() {
// end audio session
mUserInfoText!!.setText(R.string.detected_speech)
mIsListening = false
mSpeechRecognizer!!.cancel()
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment