-
我想调用
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) }
}
编辑:我发现EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS
允许我决定活动如何对沉默做出反应。但我仍然想找到一种方法来实时监控所说的内容。
也许这可以帮助您在活动调用时说出单词。
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)
}
}
}