问题
Everything is fine when I run this having English set as default language, but when I run it on any language that is not available offline I keep getting error 4 (ERROR_SERVER), even if I turn on Internet connection.
I fixed it some time ago by changing language model to LANGUAGE_MODEL_WEB_SEARCH. But I added some other features and it is not working again no matter what I change here.
What I have already tried to do:
- Read all other related questions on Stack Overflow.
- Manually set speech recognition api (I have only one available on my device).
- Added permissions to record audio and use Internet.
It might be caused, because it tries to get the offline language which does not exist. Do you know if there is any way to force SpeechRecognizer to use only online server instead of trying to connect to offline server or other way to fix it?
Code:
class CommandRecognizer(private val view: VoiceCommandsView) {
private val mSpeechRecognizer: SpeechRecognizer =
SpeechRecognizer.createSpeechRecognizer(view.getApplicationContext())
private val mSpeechRecognizerIntent: Intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
init {
create()
}
private fun create() {
mSpeechRecognizerIntent.putExtra(
RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH
)
mSpeechRecognizerIntent.putExtra(
RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault()
)
mSpeechRecognizer.setRecognitionListener(object : RecognitionListener {
override fun onReadyForSpeech(bundle: Bundle) {}
override fun onBeginningOfSpeech() {}
override fun onRmsChanged(v: Float) {}
override fun onBufferReceived(bytes: ByteArray) {}
override fun onEndOfSpeech() {}
override fun onError(i: Int) {
when (i) {
SpeechRecognizer.ERROR_SERVER -> view.onSpeechRecognizerServerError()
}
}
override fun onResults(bundle: Bundle) {
view.onCommandRecognizerResults(bundle)
}
override fun onPartialResults(bundle: Bundle) {}
override fun onEvent(i: Int, bundle: Bundle) {}
})
}
fun startListening() {
mSpeechRecognizer.startListening(mSpeechRecognizerIntent)
}
fun cancelListening() {
mSpeechRecognizer.cancel()
}
}
EDIT:
I changed some things and then I reverted them and it works again (but I have to run speech recognition few times every time I start the app, after that there is no error), despite the fact that nothing has really changed. One of the things I added was EXTRA_PREFER_OFFLINE and setting it to false. Maybe it set some global variable permanently.
I also built it on different PC. That might also be the case, because the problems started when I started working on this app on a new PC and now I tried it on a totally different one.
Anyway the app is functional now, but the error still occurs in the first few runs when starting the app. So the problem is not fully solved and this is not a stable solution.
回答1:
Finally I fixed it permanently. The only reason why it was not working was simply STAMINA mode. After disabling it everything went fine.
来源:https://stackoverflow.com/questions/58885189/speechrecognizer-error-server-when-running-not-offline-languages