I've looked at this question with answers, but as I'm new to Android programming it doesn't help me very much. If someone could help me in a dumbed down way I'd be very grateful.
So, I'm wanting to let the user enter data either with keyboard or using the built in speech recognition (that shows up in the keyboard). But how do I at the same time save the audio file if the user uses the speech recognition?
(I want to save the audio files in a database, but I guess I should look into this problem separately. It's enough for now to somehow access the audio file.)
So in my MainActivity() I have a fab button to add an entry to a database:
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, AddEditActivity.class);
i.putExtra("EXTRA_ENTRY_ID", "null");
startActivity(i);
}
This sends you to AddEditActivity(), which has a form for filling in, where the speech recognition may be done (unless the user chooses to only write with keyboard).
So would it be possible to use the code from the linked question (see below)? If so, where do I put it and how do I apply it in my project?
public void startSpeechRecognition() {
// Fire an intent to start the speech recognition activity.
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
// secret parameters that when added provide audio url in the result
intent.putExtra("android.speech.extra.GET_AUDIO_FORMAT", "audio/AMR");
intent.putExtra("android.speech.extra.GET_AUDIO", true);
startActivityForResult(intent, "<some code you choose>");
}
// handle result of speech recognition
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// the resulting text is in the getExtras:
Bundle bundle = data.getExtras();
ArrayList<String> matches = bundle.getStringArrayList(RecognizerIntent.EXTRA_RESULTS)
// the recording url is in getData:
Uri audioUri = data.getData();
ContentResolver contentResolver = getContentResolver();
InputStream filestream = contentResolver.openInputStream(audioUri);
// TODO: read audio file from inputstream
}
There is also code for speech recognition when using a custom Android keyboard here, but I wouldn't know how to adapt it to save the audio data.