Skip to content

Instantly share code, notes, and snippets.

View anshajkhare's full-sized avatar

Anshaj Khare anshajkhare

View GitHub Profile
@anshajkhare
anshajkhare / activity_main.xml
Created April 20, 2021 14:50
Layout for MainActiivty.kt
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/trigger_icon"
android:layout_width="48dp"
@anshajkhare
anshajkhare / MainActivitySpeechCallbacks.kt
Created April 20, 2021 14:46
Define speech recognition callbacks
private fun createSpeechRecognizer() {
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this)
mSpeechRecognizer?.setRecognitionListener(object : RecognitionListener {
override fun onReadyForSpeech(params: Bundle) {}
override fun onBeginningOfSpeech() {}
override fun onRmsChanged(rmsdB: Float) {}
override fun onBufferReceived(buffer: ByteArray) {}
override fun onEndOfSpeech() {
handleSpeechEnd()
@anshajkhare
anshajkhare / MainActivityCreateSpeechRecognizer.kt
Last active April 21, 2021 10:56
Create a SpeechRecognizer instance
class MainActivity : AppCompatActivity() {
private var mSpeechRecognizer: SpeechRecognizer? = null
private var mIsListening = false // this will be needed later
private var mUserInfoText: TextView? = null
private var mUserUtteranceOutput: TextView? = null
override fun onCreate(savedInstanceState: Bundle?) {
// ...
mUserUtteranceOutput = findViewById(R.id.user_utterance_output)
mUserInfoText = findViewById(R.id.user_info_text)
@anshajkhare
anshajkhare / SetClickListener.kt
Created April 20, 2021 14:44
Set click listener on microphone trigger
val trigger = findViewById<ImageView>(R.id.trigger_icon)
trigger.setOnClickListener {
// Handle audio sessions here
}
@anshajkhare
anshajkhare / MainActivityOnPermissionResult.kt
Created April 20, 2021 14:44
Handle audio permissions result
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
if (requestCode == ASR_PERMISSION_REQUEST_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// audio permission granted
Toast.makeText(this, "You can now use voice commands!", Toast.LENGTH_LONG).show()
} else {
// audio permission denied
Toast.makeText(this, "Please provide microphone permission to use voice.", Toast.LENGTH_LONG).show()
}
}
@anshajkhare
anshajkhare / MainActivityInitCommands.kt
Created April 20, 2021 14:43
Initialise voice commands list
class MainActivity : AppCompatActivity() {
// ...
private var mCommandsList: MutableList<String>? = null
override fun onCreate(savedInstanceState: Bundle?) {
// ...
initCommands()
}
private fun initCommands() {
@anshajkhare
anshajkhare / HandleCommand.kt
Created April 20, 2021 14:42
Handle spoken user commands
private fun handleCommand(command: String) {
if (mCommandsList!!.contains(command)) {
// Successful utterance, notify user
Toast.makeText(this, "Executing: $command", Toast.LENGTH_LONG).show()
} else {
// Unsucessful utterance, show failure message on screen
Toast.makeText(this, "Could not recognize command", Toast.LENGTH_LONG).show()
}
}
@anshajkhare
anshajkhare / MainActivityHandleAudio.kt
Created April 20, 2021 14:41
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()
}
@anshajkhare
anshajkhare / CreateIntent.kt
Created April 20, 2021 14:40
Create intent for speech recognition
private fun createIntent(): Intent {
val i = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
i.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true)
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-IN")
return i
}
@anshajkhare
anshajkhare / MainActivityAudioPermission.kt
Created April 20, 2021 14:39
Check for audio permissions
class MainActivity : AppCompatActivity() {
companion object {
// This constant is needed to verify the audio permission result
private const val ASR_PERMISSION_REQUEST_CODE = 0
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)