Is there a way to use the SpeechRecognizer API directly for speech input?

前端 未结 5 1888
隐瞒了意图╮
隐瞒了意图╮ 2020-11-30 01:42

The Android Dev website provides an example of doing speech input using the built-in Google Speech Input Activity. The activity displays a pre-configured pop-up with the mi

5条回答
  •  盖世英雄少女心
    2020-11-30 01:58

    You can do it as:

    import android.app.Activity
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import kotlinx.android.synthetic.main.activity_main.*
    import android.widget.Toast
    import android.content.ActivityNotFoundException
    import android.speech.RecognizerIntent
    import android.content.Intent
    
    class MainActivity : AppCompatActivity() {
        private val REQ_CODE = 100
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            speak.setOnClickListener {
                val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,  "ar-JO") //  Locale.getDefault()
                intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Need to speak")
                try {
                    startActivityForResult(intent, REQ_CODE)
                } catch (a: ActivityNotFoundException) {
                    Toast.makeText(applicationContext,
                            "Sorry your device not supported",
                            Toast.LENGTH_SHORT).show()
                }
            }
        }
    
        override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
            super.onActivityResult(requestCode, resultCode, data)
    
            when (requestCode) {
                REQ_CODE -> {
                    if (resultCode == Activity.RESULT_OK && data != null) {
                        val result = data
                                .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
                        println("result: $result")
                        text.text = result[0]
                    }
                }
            }
        }
    }
    

    The layout could be simply:

    
    
        
            
        
        
            
        
    
    

    The other way which you are asking about, is little longer, but give you more control, also does not annoying you with Google Assistance dialouge:

    1- First you need to grand permissions in the Manifest file:

        
        
    

    2- I'm consolidating all the answers above, as:

    • Create RecognitionListener class, as:
    private val TAG = "Driver-Assistant"
    
    class Listener(context: Context): RecognitionListener {
        private var ctx = context
    
        override fun onReadyForSpeech(params: Bundle?) {
            Log.d(TAG, "onReadyForSpeech")
        }
    
        override fun onRmsChanged(rmsdB: Float) {
            Log.d(TAG, "onRmsChanged")
        }
    
        override fun onBufferReceived(buffer: ByteArray?) {
            Log.d(TAG, "onBufferReceived")
        }
    
        override fun onPartialResults(partialResults: Bundle?) {
            Log.d(TAG, "onPartialResults")
        }
    
        override fun onEvent(eventType: Int, params: Bundle?) {
            Log.d(TAG, "onEvent")
        }
    
        override fun onBeginningOfSpeech() {
            Toast.makeText(ctx, "Speech started", Toast.LENGTH_LONG).show()
        }
    
        override fun onEndOfSpeech() {
            Toast.makeText(ctx, "Speech finished", Toast.LENGTH_LONG).show()
        }
    
        override fun onError(error: Int) {
            var string = when (error) {
                6 -> "No speech input"
                4 -> "Server sends error status"
                8 -> "RecognitionService busy."
                7 -> "No recognition result matched."
                1 -> "Network operation timed out."
                2 -> "Other network related errors."
                9 -> "Insufficient permissions"
                5 -> " Other client side errors."
                3 -> "Audio recording error."
                else -> "unknown!!"
            }
            Toast.makeText(ctx, "sorry error occurred: $string", Toast.LENGTH_LONG).show()
        }
    
        override fun onResults(results: Bundle?) {
            Log.d(TAG, "onResults $results")
            val data = results!!.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)
            display.text = data!![0]
        }
    }
    
    • In the main file you need to define SpeechRecognizer, adding the above listner to it, and do not forget asking for runtime permission, all together are below:
    lateinit var sr: SpeechRecognizer
    lateinit var display: TextView
    
    class MainActivity : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            display = text
    
            if (ContextCompat.checkSelfPermission(this,
                            Manifest.permission.RECORD_AUDIO)
                    != PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                                Manifest.permission.RECORD_AUDIO)) {
                } else {
                    ActivityCompat.requestPermissions(this,
                            arrayOf(Manifest.permission.RECORD_AUDIO),
                            527)
                }
            }
    
            sr = SpeechRecognizer.createSpeechRecognizer(this)
            sr.setRecognitionListener(Listener(this))
    
            speak.setOnClickListener {
                val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,  "ar-JO") //  Locale.getDefault()
                sr.startListening(intent)
            }
    
        }
    
        override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults)
            when (requestCode) {
                527  -> if (grantResults.isNotEmpty()
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    
                    Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show()
                } else {
                    Toast.makeText(this, "Permission not granted", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }
    

提交回复
热议问题