Guest User

Untitled

a guest
Jun 21st, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. package com.shellmonger.apps.speechtranslator.ui
  2.  
  3. import android.content.Intent
  4. import android.support.v7.app.AppCompatActivity
  5. import android.os.Bundle
  6. import android.speech.RecognizerIntent
  7. import android.speech.SpeechRecognizer
  8. import android.util.Log
  9. import com.shellmonger.apps.speechtranslator.R
  10. import com.shellmonger.apps.speechtranslator.TAG
  11. import kotlinx.android.synthetic.main.activity_main.*
  12. import java.lang.Exception
  13.  
  14. /**
  15. * The main activity is the first (and only) activity that is displayed for this app
  16. */
  17. class MainActivity : AppCompatActivity() {
  18. companion object {
  19. const val REQUEST_SPEECH_RECOGNIZER = 1000
  20. }
  21.  
  22. override fun onCreate(savedInstanceState: Bundle?) {
  23. super.onCreate(savedInstanceState)
  24. setContentView(R.layout.activity_main)
  25.  
  26. initializeClient()
  27.  
  28. // Wire up the button to start recognizing the speech
  29. btn_start_recognizer.setOnClickListener {
  30. val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH).apply {
  31. putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
  32. }
  33. startActivityForResult(intent, REQUEST_SPEECH_RECOGNIZER)
  34. }
  35.  
  36. // The button is (by default) disabled, so you can't click it. Enable it if the
  37. // isRecognitionAvailable() supported
  38. if (SpeechRecognizer.isRecognitionAvailable(this)) {
  39. btn_start_recognizer.isEnabled = true
  40. } else {
  41. Log.i(TAG, "Speech Recognition is not available")
  42. }
  43. }
  44.  
  45. override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
  46. super.onActivityResult(requestCode, resultCode, data)
  47.  
  48. if (requestCode == REQUEST_SPEECH_RECOGNIZER) {
  49. if (resultCode == RESULT_OK) {
  50. data?.let {
  51. val results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
  52. recorded_speech.text = results[0]
  53. }
  54. }
  55. }
  56. }
  57.  
  58. private fun initializeClient() {
  59. }
  60. }
Add Comment
Please, Sign In to add comment