3

I have working solution for voice recognition and saving the audio file, but the code works only on some devices.

I have tried running this code on few devices. It seems that this solution works on android 5.0, but doesn't work on higher versions.

I'm using Intent class to do that:

System.out.println("startVoiceInput");
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
                intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Hello, How can I help you?");
                intent.putExtra("android.speech.extra.GET_AUDIO_FORMAT", "audio/AMR");
                intent.putExtra("android.speech.extra.GET_AUDIO", true);
                try {
                    startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
                } catch (ActivityNotFoundException a) {
                    a.printStackTrace();
                }

Recognition itself works on every device (without those lines:

intent.putExtra("android.speech.extra.GET_AUDIO_FORMAT", "audio/AMR");
intent.putExtra("android.speech.extra.GET_AUDIO", true);

).

But with that lines, necessary to catch audio stream to save it on device, the google popup doesn't appear. I don't get any error, but this log:

D/ViewRootImpl@e232f4[RecordingActivity]: ViewPostIme pointer 0
D/ViewRootImpl@e232f4[RecordingActivity]: ViewPostIme pointer 1
I/System.out: startVoiceInput
D/ViewRootImpl@e232f4[RecordingActivity]: MSG_WINDOW_FOCUS_CHANGED 0
I/System.out: onActivityResult
D/ViewRootImpl@e232f4[RecordingActivity]: Relayout returned: old=[0,0][1080,1920] new=[0,0][1080,1920] result=0x1 surface={valid=true 527892242432} changed=false
   MSG_WINDOW_FOCUS_CHANGED 1
V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@4dc5d3e nm : com.example.acces ic=null
I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
Azarius
  • 31
  • 3
  • So you're using two extras that aren't even part of RecognizerIntent (thus aren't official in any way) and just hoping they work? That really isn't going to work well. If the phone is using an old version, it won't work. If the phone is using a voice recognition service other than Google's, it won't work (and I know many used to use Nuance's). Don't rely on non-standard extras to intents working unless you control the app that will be responding to it. – Gabe Sechan Apr 01 '19 at 14:13
  • I have found that way to do that on stackoverflow, so... Anyway, do You know any other solution to that or not? I haven't found any "standard" way to recognize voice input, and also save that input as audio file. – Azarius Apr 01 '19 at 14:28
  • There isn't one. It's just functionality you can't get on all devices, sort of worrying your own recognizer – Gabe Sechan Apr 01 '19 at 14:29

1 Answers1

0

Google Keep still uses these secret parameters. You can check it as described here. So it should work.

I checked it in my application. Google popup does not appear if your application does not have RECORD_AUDIO permission. If you add RECORD_AUDIO permission to AndroidManifest.xml then the solution described here works.

Andrey Epifantsev
  • 976
  • 1
  • 11
  • 27