当我开始我的活动或片段时,我想开始语音识别,当用户说话时,如果它与某个字符串匹配,则会显示结果。结果出来后它应该再次开始识别
这是我尝试过的代码。它是按一个按钮启动的,当用户不说话时它会生成错误,之后它不会识别任何东西,直到我按下按钮再次开始。
我知道上面是语音识别的默认情况,但我怎样才能连续进行?
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/speak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?selectableItemBackground"
android:src="@android:drawable/ic_btn_speak_now"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var mSpeechRecognizer: SpeechRecognizer
private lateinit var mSpeechRecognizerIntent: Intent
private var mIslistening = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
speak.setOnClickListener {
mIslistening = true;
mSpeechRecognizer.stopListening()
val listener: SpeechRecognitionListener = SpeechRecognitionListener()
mSpeechRecognizer.setRecognitionListener(listener)
mSpeechRecognizer.startListening(mSpeechRecognizerIntent)
}
speechToText()
}
override fun onDestroy() {
super.onDestroy()
if (mSpeechRecognizer != null) {
mSpeechRecognizer.destroy()
}
}
private fun speechToText() {
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this)
mSpeechRecognizerIntent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
mSpeechRecognizerIntent.putExtra(
RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
)
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.packageName)
}
inner class SpeechRecognitionListener() : RecognitionListener {
override fun onBeginningOfSpeech() {
}
override fun onBufferReceived(buffer: ByteArray) {}
override fun onEndOfSpeech() {
Log.e("TAG", "onEndOfSpeech");
}
override fun onError(error: Int) {
Log.d("TAG", "error = $error");
}
override fun onEvent(eventType: Int, params: Bundle) {}
override fun onPartialResults(partialResults: Bundle) {}
override fun onReadyForSpeech(params: Bundle) {
Log.d("TAG", "onReadyForSpeech") //$NON-NLS-1$
}
override fun onResults(results: Bundle) {
//Log.d(TAG, "onResults"); //$NON-NLS-1$
val matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)
matches?.let {
if (matches.isNotEmpty()) {
text.text = matches[0].toString()
Log.e("Test", matches[0].toString());
}
}
// matches are the return values of speech recognition engine
// Use these values for whatever you wish to do
}
override fun onRmsChanged(rmsdB: Float) {}
}
}
可能想查看 https://github.com/kfirtaizi/kotlin-silence-aware-vad-recorder 以获得另一个实现实时语音识别录音的工具。