This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val trigger = findViewById<ImageView>(R.id.trigger_icon) | |
| trigger.setOnClickListener { | |
| // Handle audio sessions here | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MainActivity : AppCompatActivity() { | |
| // ... | |
| private var mCommandsList: MutableList<String>? = null | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| // ... | |
| initCommands() | |
| } | |
| private fun initCommands() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MainActivity : AppCompatActivity() { | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| // ... | |
| trigger.setOnClickListener { | |
| // Handle audio sessions here | |
| if (mIsListening) { | |
| handleSpeechEnd() | |
| } else { | |
| handleSpeechBegin() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
NewerOlder