Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.shellmonger.apps.speechtranslator.ui
- import android.content.Intent
- import android.support.v7.app.AppCompatActivity
- import android.os.Bundle
- import android.speech.RecognizerIntent
- import android.speech.SpeechRecognizer
- import android.util.Log
- import com.shellmonger.apps.speechtranslator.R
- import com.shellmonger.apps.speechtranslator.TAG
- import kotlinx.android.synthetic.main.activity_main.*
- import java.lang.Exception
- /**
- * The main activity is the first (and only) activity that is displayed for this app
- */
- class MainActivity : AppCompatActivity() {
- companion object {
- const val REQUEST_SPEECH_RECOGNIZER = 1000
- }
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- initializeClient()
- // Wire up the button to start recognizing the speech
- btn_start_recognizer.setOnClickListener {
- val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH).apply {
- putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
- }
- startActivityForResult(intent, REQUEST_SPEECH_RECOGNIZER)
- }
- // The button is (by default) disabled, so you can't click it. Enable it if the
- // isRecognitionAvailable() supported
- if (SpeechRecognizer.isRecognitionAvailable(this)) {
- btn_start_recognizer.isEnabled = true
- } else {
- Log.i(TAG, "Speech Recognition is not available")
- }
- }
- override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
- super.onActivityResult(requestCode, resultCode, data)
- if (requestCode == REQUEST_SPEECH_RECOGNIZER) {
- if (resultCode == RESULT_OK) {
- data?.let {
- val results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
- recorded_speech.text = results[0]
- }
- }
- }
- }
- private fun initializeClient() {
- }
- }
Add Comment
Please, Sign In to add comment