识别器内容在离线模式下

时间:2014-07-14 09:23:07

标签: android voice-recognition

我使用的RecognizerIntent工作得很好(参见下面的函数)。我想要的是在离线模式下运行。如果RecognizerIntent不是一个好的解决方案,那么替代方案是什么?另外,如果是另一种解决方案,我会想要设置语言......谢谢

public void speak(View view) {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        // Specify the calling package to identify your application
        intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass()
                .getPackage().getName());

        // Display an hint to the user about what he should say.
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT, metTextHint.getText()
                .toString());

        // Given an hint to the recognizer about what the user is going to say
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);

        // If number of Matches is not selected then return show toast message
        if (msTextMatches.getSelectedItemPosition() == AdapterView.INVALID_POSITION) {
            Toast.makeText(this, "Please select No. of Matches from spinner",
                    Toast.LENGTH_SHORT).show();
            return;
        }

        int noOfMatches = Integer.parseInt(msTextMatches.getSelectedItem()
                .toString());
        // Specify how many results you want to receive. The results will be
        // sorted where the first result is the one with higher confidence.

        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, noOfMatches);

        startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
    }

2 个答案:

答案 0 :(得分:1)

您需要定义语言english US--

recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");

现在将手机置于“飞行模式”并进行测试。

注意 - 仅在API-16 +

中脱机工作

答案 1 :(得分:0)

intent.putExtra(RecognizerIntent.EXTRA_PREFER_OFFLINE, true);