我在按下按钮时通过调用方法startListening来使用SpeechRecognizer类,但是我收到错误。首先调用onReadyForSpeech,onBeginningOfSpeech,onEndofSpeech上的回调方法(立即),最后调用onError,错误代码为2" ERROR_NETWORK"叫做。当我使用startActivityForResult直接调用intent时,它可以工作。但我想摆脱耗时的弹出对话框。 我有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.");
}
}
答案 0 :(得分:1)
我自己解决了。问题是,美国的语言包有更新。我不得不手动加载它(在我的手机的语言设置中)。之后错误ERROR_NETWORK消失了。