使用 SpeechRecognizer 会产生 ERROR_NETWORK(值 2)
Posted
技术标签:
【中文标题】使用 SpeechRecognizer 会产生 ERROR_NETWORK(值 2)【英文标题】:Use of SpeechRecognizer produces ERROR_NETWORK (Value 2) 【发布时间】:2014-04-07 15:22:40 【问题描述】:我通过在按下按钮时调用方法 startListening 来使用 SpeechRecognizer 类,但我得到了一个错误。首先(立即)调用回调方法 onReadyForSpeech、onBeginningOfSpeech、onEndofSpeech,最后调用带有错误代码 2“ERROR_NETWORK”的 onError。当我使用 startActivityForResult 直接调用意图时,它可以工作。但我想摆脱耗时的弹出对话框。 我设置了 RECORD_AUDIO 权限。
我不确定,但可能是想从互联网上加载一些东西。为此,我尝试添加 INTERNET-Permission,但它也不起作用。
代码如下:
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.");
【问题讨论】:
识别器是否可能要加载缺少的语言包? 我查看了源代码(linkcode
。我看不到,这里设置了ERROR_NETWORK。
你可能想从 UI 线程调用 Start 方法,有类似的吗?
在我看来 startListening 调用已经在 UI 线程中完成(因为 Start 是按钮的 on_click 处理程序)。
我的问题是,RecognitionListener 不能正常工作。它使用错误代码 2 调用 onError。
【参考方案1】:
我自己解决了。问题是,美国的语言包有更新。我必须手动加载它(在我手机的语言设置中)。之后,错误 ERROR_NETWORK 消失了。
【讨论】:
似乎无关。语言包用于离线识别。以上是关于使用 SpeechRecognizer 会产生 ERROR_NETWORK(值 2)的主要内容,如果未能解决你的问题,请参考以下文章
有没有办法将音频字节发送到 SpeechRecognizer
面向 Android 11 时 SpeechRecognizer 不可用
Android科大讯飞——语音识别 解决"SpeechRecognizer.createRecognizer()获取的SpeechRecognizer对象为null"问题