问题描述
我使用的是类SpeechRecognizer通过调用当按钮是pressed的方法startListening,但我得到一个错误。首先回调方法onReadyForSpeech,onBeginningOfSpeech,onEndofSpeech被称为(立即),并在年底的onError的错误code 2ERROR_NETWORK之称。当我直接调用的意图通过startActivityForResult,它的工作原理。但我想摆脱耗时的弹出对话框。我有RECORD_AUDIO权限集。
我不知道,但也许是故意要加载从互联网上的东西。对于这一点,我试图添加Internet-权限,但它并没有擦出火花。
下面的code:
公共类MainActivity扩展ActionBarActivity实现RecognitionListener
{
私人SpeechRecognizer speechRecognizer;
公共无效onReadyForSpeech(包PARAMS)
{
AddLog(onReadyForSpeech称为);
}
公共无效onBeginningOfSpeech()
{
AddLog(onBeginningOfSpeech称为);
}
公共无效onRmsChanged(浮动rmsdB)
{
AddLog(onRmsChanged称为);
}
公共无效onBufferReceived(byte []的缓冲区)
{
AddLog(onBufferReceived称为);
}
公共无效onEndOfSpeech()
{
AddLog(onEndOfSpeech称为);
}
公共无效onerror的(INT错误)
{
AddLog(的String.Format(之称的onError ID为:%D,错误));
}
公共无效onResults(捆绑结果)
{
字符串str =新的String();
ArrayList的<字符串>数据= results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
的for(int i = 0; I< data.size();我++)
{
海峡+ = data.get(ⅰ);
}
最后弦乐strAnswer =海峡;
AddLog(的String.Format(答案是:%s,strAnswer));
}
公共无效onPartialResults(包psrtialResults)
{
AddLog(onPartialResults称为);
}
公共无效的onEvent(INT事件类型,捆绑PARAMS)
{
AddLog(ONEVENT称为);
}
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
如果(savedInstanceState == NULL){
getSupportFragmentManager()的BeginTransaction()
。新增(R.id.container,新PlaceholderFragment())
。承诺();
}
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(本);
speechRecognizer.setRecognitionListener(本);
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
//充气菜单;这增加了项目操作栏,如果它是present。
。getMenuInflater()膨胀(R.menu.main,菜单);
返回true;
}
@覆盖
公共布尔onOptionsItemSelected(菜单项项){
//处理动作栏项目点击这里。将操作栏
//自动在主/向上按钮操作的点击,只要
//你在AndroidManifest.xml中指定一个父活动。
INT的id = item.getItemId();
如果(ID == R.id.action_settings){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
/ **
*包含一个简单的视图的占位符片段。
* /
公共静态类PlaceholderFragment扩展片段
{
公共PlaceholderFragment()
{
}
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
捆绑savedInstanceState){
查看rootView = inflater.inflate(R.layout.fragment_main,集装箱,假);
返回rootView;
}
}
公共无效AddLog(最终文本字符串)
{
TextView的txtLogView =(TextView中)findViewById(R.id.textViewLog);
如果(txtLogView!= NULL)
{
txtLogView.append(文字+\ N);
}
}
//仅用于测试
@覆盖
保护无效onActivityResult(INT申请code,INT结果code,意图数据)
{
如果(要求code == 1安培;&安培;结果code == RESULT_OK)
{
ArrayList的<字符串>比赛= data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
AddLog(matches.get(0));
}
super.onActivityResult(要求code,因此code,数据);
}
公共无效启动(查看视图)
{
AddLog(开始pressed。);
意向意图=新的意图(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);
//仅用于测试
// startActivityForResult(意向,1);
speechRecognizer.startListening(意向);
AddLog(startListening称为);
}
}
我由我自己解决它。问题是,对于美国的语言包有更新。我不得不用手加载它(在我的手机的语言设置)。在此之后,错误ERROR_NETWORK消失了。
I am using the class SpeechRecognizer by calling the method startListening when a button is pressed, but I get an error. First the callback methods onReadyForSpeech, onBeginningOfSpeech, onEndofSpeech are called (immediately) and at the end onError with the errorcode 2 "ERROR_NETWORK" is called. When I call the intent directly by using startActivityForResult, it works. But I want to get rid of the time consuming popup dialog.I have the RECORD_AUDIO permission set.
I am not sure, but perhaps the intent want to load something from the internet. For that, I tried to add the INTERNET-Permission, but it did not work either.
Here the code:
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.");
}
}
I solved it by myself. The problem was, that the language pack for US had an update. I had to load it by hand (in the language settings of my mobile). After that the error ERROR_NETWORK disappeared.
这篇关于使用SpeechRecognizer的生产ERROR_NETWORK(价值2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!