1

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.

Community
  • 1
  • 1
Ingrid
  • 516
  • 6
  • 18
  • It is possible, but not easy, you need to modify keyboard code itself, not the code that invokes keyboard. – Nikolay Shmyrev Jan 08 '17 at 18:43
  • Possible duplicate of [Android Custom Keyboard with SpeechRecognizer](http://stackoverflow.com/questions/41448163/android-custom-keyboard-with-speechrecognizer) – Nikolay Shmyrev Jan 08 '17 at 18:43
  • @NikolayShmyrev Thanks, I'll look into it. Maybe it would be easier for me to have a solution where the user first chooses either standard keyboard input or speech recognition, so I don't need to modify the keyboard... – Ingrid Jan 09 '17 at 12:18
  • Yes, you can go this way. I'm not sure about user experience for such implementation though. – Nikolay Shmyrev Jan 09 '17 at 13:00
  • sample proj. ( record audio and speech recognize ) but its more complex in use of external libs... https://github.com/rowntreerob/Google-speech-Opus-Recorder – Robert Rowntree Jan 09 '17 at 14:57

0 Answers0