如何配置RecognizerIntent以供脱机使用

时间:2016-05-31 06:25:28

标签: android speech-to-text offline-mode

我有一些代码可以将语音转换为文本,但只有在设备具有互联网连接时才会运行。我想看到我的代码脱机工作,为此我已经从stackoverflow和其他来源经历了几个代码,但我的代码仍然无法在离线模式下工作。

该设备是Moto X Play。启用离线语音识别功能。

这是我的代码。

 private void promptSpeechInput() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    //intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,"en-US");
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE,"en-US");
    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,this.getPackageName());
    intent.putExtra(RecognizerIntent.EXTRA_PREFER_OFFLINE,true);
    //intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
    //intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT,getString(R.string.speech_prompt));
    try {
        startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
    } catch (ActivityNotFoundException a) {
        Toast.makeText(getApplicationContext(),
                getString(R.string.speech_not_supported),
                Toast.LENGTH_SHORT).show();
    }
}

需要帮助才能理解它,我想我错过了一些东西。

观察 没有其他应用允许离线模式。

1 个答案:

答案 0 :(得分:-2)

我使用了下面的代码并且它在离线状态下进行:关注它(http://www.androidhive.info/2014/07/android-speech-to-text-tutorial/

 Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
        //intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US"));
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT,getString(R.string.speech_prompt));
        try {
            startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
        } catch (ActivityNotFoundException a) {
            Toast.makeText(getApplicationContext(),getString(R.string.speech_not_supported),Toast.LENGTH_SHORT).show();
        }