3

I have followed this post to implement an app and save Google voice search audio. I have used @Iftah's code to save amr file to storage. This works on Android phones, but when I try this on Android TV/STB there is no URI getting generated for the audio.

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
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {

        Log.d("URI", data.toString());
        Log.d("URI", ""+requestCode);
        Log.d("URI", ""+resultCode);
        Uri audioUri = data.getData();
        ContentResolver contentResolver = getContentResolver();
        InputStream filestream = null;
        try {
            filestream= contentResolver.openInputStream(audioUri);
        } catch (FileNotFoundException e) {
            Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }

        String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/rec1.amr";

        byte[] buffer = new byte[0];
        try {
            buffer = new byte[filestream.available()];
            filestream.read(buffer);
            OutputStream outStream = new FileOutputStream(filePath);
            outStream.write(buffer);
        } catch (IOException | NullPointerException e) {
            Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }

        Bundle bundle = data.getExtras();
        // Populate the wordsList with the String values the recognition engine thought it heard
        final ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

        if (!matches.isEmpty()) {
            String Query = matches.get(0);
            ed.setText(Query);
            //speak.setEnabled(false);
        }

    }
    super.onActivityResult(requestCode, resultCode, data);
}

I logged Intent data and here is the result from phone and TV:

TV/STB:

C:\Users\AndroidStudioProjects\voice\app\build\outputs\apk\debug>adb logcat | findstr URI
08-27 13:32:25.915 11353 11353 D URI     : Intent { (has extras) }
08-27 13:32:25.915 11353 11353 D URI     : 1234
08-27 13:32:25.915 11353 11353 D URI     : -1

Android Phone:

C:\Users\AndroidStudioProjects\voice\app\build\outputs\apk\debug>adb -d logcat | findstr URI
08-27 13:33:22.585 24064 24064 D URI     : Intent { dat=content://com.google.android.googlequicksearchbox.AudioRecordingProvider/my_recordings/recording.amr flg=0x1 (has extras) }
08-27 13:33:22.585 24064 24064 D URI     : 1234
08-27 13:33:22.585 24064 24064 D URI     : -1

So from above we can see STB/TV is not retuning audio URI.
Both devices are running Android 9 with latest speech recognition apps.
Is it not possible to save audio on TV/STB?

  PS: I am very new to App development. So sorry for any silly mistakes :) 
  • 1
    Also I have enabled "Include audio recordings" in account settings(if that matters) – Santhosh Rao Aug 27 '21 at 09:42
  • Did you find a way to save or record the audio? – Ranjith Kumar Jun 11 '23 at 08:29
  • 1
    @RanjithKumar I reached out to Google and they suggested that I either create a app that gets triggered when voice search is triggered and record the audio stream via audio recorder library or just use record option in Developer options available on and above Android 11. Also if you want this on a phone, above code works without any issue. – Santhosh Rao Jun 12 '23 at 13:30
  • @Santosh Rao Thanks for the clarification. I didnt know the record option from developer options will work.. Can the recorded audio be dumped via adb bugreport? – Ranjith Kumar Aug 05 '23 at 07:16
  • @RanjithKumar the recorded audio is saved to root sdcard folder. – Santhosh Rao Aug 24 '23 at 15:14

0 Answers0