运行非脱机语言时的SpeechRecognizer ERROR_SERVER [英] SpeechRecognizer ERROR_SERVER when running not offline languages

查看:135
本文介绍了运行非脱机语言时的SpeechRecognizer ERROR_SERVER的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将英语设置为默认语言时,一切都很好,但是当我使用任何无法脱机使用的语言来运行它时,即使打开Internet连接,我仍然会收到错误4(ERROR_SERVER).

我前一段时间通过将语言模型更改为LANGUAGE_MODEL_WEB_SEARCH来解决此问题.但是我添加了一些其他功能,无论我在此处进行什么更改,它都无法再次使用.

我已经尝试做的事情:

  • 阅读有关堆栈溢出的所有其他相关问题.
  • 手动设置语音识别api(我的设备上只有一个).
  • 添加了录制音频和使用Internet的权限.

可能是由于它试图获取不存在的脱机语言而引起的.您是否知道有什么方法可以迫使SpeechRecognizer仅使用在线服务器,而不是尝试连接到脱机服务器或其他方法来修复它?

代码:

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()
    }
}

我进行了一些更改,然后还原了它们,然后又可以正常工作(但是每次启动应用程序时我都必须运行语音识别几次,之后才不会出现错误),尽管实际上并没有任何改变.我添加的一件事是EXTRA_PREFER_OFFLINE并将其设置为false.也许它会永久设置一些全局变量.

我也在不同的PC上构建了它.也可能是这种情况,因为当我开始在新PC上使用此应用程序,而现在又在另一台PC上尝试该应用程序时,问题就开始了.

无论如何该应用程序现在都可以运行,但是在启动该应用程序时,在前几次运行中仍然会出现错误.因此,该问题尚未完全解决,并且不是一个稳定的解决方案.

解决方案

最后,我将其永久修复.它不起作用的唯一原因只是STAMINA模式.禁用它之后,一切都很好.

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.

解决方案

Finally I fixed it permanently. The only reason why it was not working was simply STAMINA mode. After disabling it everything went fine.

这篇关于运行非脱机语言时的SpeechRecognizer ERROR_SERVER的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆