Android 使用语音识别器会产生网络错误(值2)

Android 使用语音识别器会产生网络错误(值2),android,speech-recognition,Android,Speech Recognition,我正在使用SpeechRecognizer类,当按下按钮时调用startListening方法,但我得到一个错误。首先(立即)调用readyforspeech、onbeginingofspeech、onEndofSpeech上的回调方法,最后调用一个错误代码为2“ERROR_NETWORK”的错误。当我使用startActivityForResult直接调用intent时,它是有效的。但我想摆脱耗时的弹出对话框。 我已设置录制音频权限 我不确定,但可能是想从互联网上加载一些东西。为此,我尝试添加

我正在使用SpeechRecognizer类,当按下按钮时调用startListening方法,但我得到一个错误。首先(立即)调用readyforspeech、onbeginingofspeech、onEndofSpeech上的回调方法,最后调用一个错误代码为2“ERROR_NETWORK”的错误。当我使用startActivityForResult直接调用intent时,它是有效的。但我想摆脱耗时的弹出对话框。 我已设置录制音频权限

我不确定,但可能是想从互联网上加载一些东西。为此,我尝试添加互联网许可,但也没有成功

代码如下:

public class MainActivity extends ActionBarActivity implements RecognitionListener
{   
    private SpeechRecognizer speechRecognizer;

    public void onReadyForSpeech(Bundle params)
    {
        AddLog("onReadyForSpeech called.");
    }

    public void onBeginningOfSpeech()
    {
        AddLog("onBeginningOfSpeech called.");
    }

    public void onRmsChanged(float rmsdB)
    {
        AddLog("onRmsChanged called.");
    }

    public void onBufferReceived(byte[] buffer)
    {
        AddLog("onBufferReceived called.");
    }

    public void onEndOfSpeech()
    {
        AddLog("onEndOfSpeech called.");
    }

    public void onError(int error)
    {
        AddLog(String.format("onError called with id: %d.", error));
    }

    public void onResults(Bundle results)
    {
        String str = new String();
        ArrayList<String> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
        for(int i = 0; i < data.size(); i++)
        {
            str += data.get(i);
        }

        final String strAnswer = str;

        AddLog(String.format("Answer is %s", strAnswer));
    }

    public void onPartialResults(Bundle psrtialResults)
    {
        AddLog("onPartialResults called.");
    }

    public void onEvent(int eventType, Bundle params)
    {
        AddLog("onEvent called.");
    }           

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }

        speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);        
        speechRecognizer.setRecognitionListener(this);        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment
    {
        public PlaceholderFragment()
        {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);

            return rootView;
        }
    }

    public void AddLog(final String text)
    {
        TextView txtLogView = (TextView) findViewById(R.id.textViewLog);
        if (txtLogView != null)
        {
            txtLogView.append(text + "\n");
        }
    }

    // Only for test    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (requestCode == 1 && resultCode == RESULT_OK)
        {
            ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

            AddLog(matches.get(0));
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    public void Start(View view)
    {
        AddLog("Start pressed.");

        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getPackageName());
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");

        // Only for test
        //startActivityForResult(intent, 1);

        speechRecognizer.startListening(intent);

        AddLog("startListening called.");
    }    
}
公共类MainActivity扩展了ActionBarActivity实现了RecognitionListener
{   
私人语音识别器语音识别器;
ReadyForSpeech上的公共void(Bundle参数)
{
AddLog(“onReadyForSpeech调用”);
}
开始时的公共无效fSpeech()
{
AddLog(“onbeginingofspeech调用”);
}
在RMSCHANGED上的公共无效(浮动rmsdB)
{
AddLog(“onRmsChanged调用”);
}
已接收公共无效onBufferReceived(字节[]缓冲区)
{
AddLog(“onBufferReceived called”);
}
公共无效onEndOfSpeech()
{
AddLog(“名为onEndOfSpeech”);
}
公共无效onError(内部错误)
{
AddLog(String.format(“使用id%d调用的OneError”,错误));
}
公共结果(捆绑结果)
{
String str=新字符串();
ArrayList data=results.getStringArrayList(SpeechRecognitor.results\u RECOGNITION);
对于(int i=0;i
我自己解决了。问题是,我们的语言包有一个更新。我必须手动加载(在手机的语言设置中)。之后,错误网络消失。

识别器是否可能要加载丢失的语言包?我查看了源代码(
code
。我看不出,这个错误是在这里设置的。你可能想从UI线程调用Start方法,有类似的事情吗?在我看来,startListening调用已经在UI线程中完成了(因为Start是一个按钮上的点击处理程序)。我的问题是,RecognitionListener无法正常工作。它调用OneError,错误代码为2。似乎不相关。语言包用于脱机识别。