在Android中,当使用SpeechRecognizer时,我注意到SpeechRecognizer会在几秒钟后自动停止收听。
我正在开发一个需要用户连续语音输入的应用程序。我们的应用程序需要在没有互联网的情况下工作。对于语音识别,我们使用SpeechRecognizer类。
下面是实现:(在Kotlin中)
var recognizer = SpeechRecognizer.createSpeechRecognizer(this.applicationContext)
recognizer!!.setRecognitionListener(RecognitionListener(this))
开始收听
val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "com.taazaa.texttospeechdemo")
recognizer!!.startListening(intent)
这很好,直到用户在讲话前暂停一下。
我需要执行以下操作:
在用户手动停止识别之前,SpeechRecognizer不应停止收听。
recognizer !!。stopListening()
每次识别开始或停止时,设备都会发出蜂鸣声,需要停止。
应用应大多在脱机模式下工作。
让我知道我在做错什么,以及需要采取哪些措施来支持以上两点。 Google Bolo正在这样做,所以可能有办法。 Google bolo:https://play.google.com/store/apps/details?id=com.google.android.apps.seekh&hl=en_IN
我尝试了许多链接,下面提到其中一些:
Offline Speech Recognition in Android
Offline Speech Recognition In Android (JellyBean)
答案 0 :(得分:0)
您可以看一下在没有互联网的情况下使用SpeechRecognizer的示例:
*顺便说一下,它在日本。
关于持续收听...使用Android的SpeechRecognizer库有点棘手。我很久以前就已经做到了,问题出在设备的麦克风上。如果它在某种程度上被某物阻塞或打断,比如说入口被某物遮盖或该人正在使用免提装置(蓝牙,这也是另一种考虑),那么结果将不会100%成功。 / p>
我建议您使用组件服务。这将使SpeechRecognizer继续进行。但是考虑一下状态:
**在处理过程中,该应用可能无法监听,您也将需要处理。
由于组件Service在后台运行,因此您需要将此与您的活动或片段(也许使用Binders或Broadcast,取决于您的应用程序)联系起来
class ServiceSpeech : Service(), RecognitionListener {
val mVoiceBinder: IBinder = LocalBinder()
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
return START_STICKY
}
inner class LocalBinder : Binder() {
fun getService(): ServiceSpeech? {
return this@ServiceSpeech
}
}
/*
*
* create the methods for:
* build speechrecognizer
* startlistening
* stoplistening
* listeningAgain
* send the results to activity/fragment
*
* */
override fun onBind(p0: Intent?): IBinder? {
return mVoiceBinder
}
override fun onReadyForSpeech(p0: Bundle?) {
//important
}
override fun onBeginningOfSpeech() {
//important
}
override fun onError(p0: Int) {
//important, you could check SpeechRecognizer.ERROR_* to handle the errors
}
override fun onRmsChanged(p0: Float) {
//handle some events, will give you the sound intensity
}
override fun onPartialResults(p0: Bundle?) {
//important
}
override fun onResults(p0: Bundle?) {
//important
}
override fun onEndOfSpeech() {
//important
}
override fun onEvent(p0: Int, p1: Bundle?) {
}
override fun onBufferReceived(p0: ByteArray?) {
}
}
希望这对您有所帮助。