I am creating an app that using google's api for converting speech to text. The following code works perfectly when I have it set as the main activity. However I am now creating a new app and have copy pasted the xml layout and java file as an activity called display_1
. My issue now is when I navigate to this window through the app, I expect voice recognition to begin after 500ms and it in fact does but I get a message saying 'Can't open Microphone'.
I have added the correct permissions into the gradle file and have set the correct min API level. Please not my main activity for the app uses PocketSphinx sdk and then this second activity called display_1
uses google api which implements onClickListener
If someone can tell me what is causing me to get the 'Can't Open Microphone' warning that will be appreciated. Please note wifi etc are connected. Below is the java file:
public class Display_1 extends Activity implements View.OnClickListener {
ListView lv;
static final int check = 1111;
Button b;
EditText a;
Button c;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.display_1);
lv = (ListView) findViewById(R.id.lvVoiceReturn_1);
a = (EditText) findViewById(R.id.TFusername_1);
b = (Button) findViewById(R.id.bVoice_1);
c = (Button)findViewById(R.id.Blogin_1);
b.setOnClickListener(this);
//This now handles an automatic press of the bVoice button 1 second after the activity is opened
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
b.callOnClick();
}
}, 1000);
}
public void onButtonClick(View v) {
if (v.getId() == R.id.Blogin_1) {
String str = a.getText().toString();
//Go to the relevant page if any part of the phrase or word entered in the 'EditText' field contains 'next' which is not case sensitive
if (str.toLowerCase().contains("next")) {
Intent userintent = new Intent(Display_1.this, Display_2.class);
startActivity(userintent);
}else {
Toast.makeText(getApplicationContext(), "Incorrect Information", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onClick(View v) {
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Please Repeat Again");
startActivityForResult(i, check);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == check && resultCode == RESULT_OK) {
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));
a.setText((String) lv.getItemAtPosition(0)); //Get the first phrase in the first row of list view
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
c.performClick();
}
}, 500); //Automatically click the 'Blogin' button after 500ms
}
super.onActivityResult(requestCode, resultCode, data);
}
}
The following are the permission I have set in the android manifest file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />