我想调用 RecognizerIntent
目前的android识别器意图是在识别出输入后声音停止时关闭。例如:如果识别到 "hi",如果用户在这之后一两秒内没有说任何话,活动就会关闭。我有什么办法可以阻止活动关闭吗? 并永远持续下去,直到我选择关闭它(也许通过点击按钮第二次?
我也希望得到实时的数据,以了解正在说什么,而不是在活动关闭后将其作为额外的数据传递给它。
package com.example.myapplication
import android.app.Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.speech.RecognizerIntent
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_recordinit.*
import java.lang.Exception
import java.util.*
class Recordinit : AppCompatActivity() {
val REQUESTCODE3 = 4
fun audioaction(){
val speechtotextintent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
speechtotextintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.EXTRA_LANGUAGE_MODEL)
speechtotextintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE , Locale.getDefault())
speechtotextintent.putExtra(RecognizerIntent.EXTRA_PROMPT , "Click To Begin Listening on the threat!!")
try {
//check to see if the activity can work on this device... and it meets the requirements
startActivityForResult(speechtotextintent , REQUESTCODE3 )
}
//if there is any errors we will let the user know in a popup message.
catch (e : Exception){
Toast.makeText(this , e.message , Toast.LENGTH_SHORT ).show()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_recordinit)
microphonebutton.setOnClickListener{
audioaction()
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (REQUESTCODE3 == requestCode ){
when (resultCode == Activity.RESULT_OK ){
}
}
super.onActivityResult(requestCode, resultCode, data)
}
}
EDIT: 我发现 EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS
可以让我决定活动对沉默的反应。但我还是想找到一种方法来实时监控正在说的内容,我想调用RecognizerIntent,让它永远循环,直到用户决定关闭它。
也许这能帮助你获得活动调用时说的话。
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_CODE10) {
if (resultCode == RESULT_OK && data != null) {
var result : ArrayList<String> = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
}
}
}