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.");
}
}