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 :)