1

I am trying to create an app that will allow a user to upload an audio file created by Googles speech to text to a server. I have managed to get the URI for the audio file but how do I access it or convert it to a listenable format? I tried to play it back but nothing so far. This is what I have.

My Speech to text

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode){
        case REQ_CODE_SPEECH_INPUT: {
            Bundle bundle = data.getExtras();
            if(resultCode==RESULT_OK && null!= data){
                ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                vtt.setText(result.get(0));
                Uri audioUri = data.getData();
                uri.setText(String.valueOf(audioUri));
                audioPath = uri.getText().toString();

            }
            break;

        }
    }
}

When I tried to play the audioPath variable. Nothing came out. How do I convert it to a listenable format?

Example of a uri that I got

content://com.google.android.googlequicksearchbox.AudioProvider/NoteToSelfOriginalAudio1.amr

Thank you for your help

I found somewhere that I should use content resolver and do something with the inputstream but Im not sure what.

JianYA
  • 2,750
  • 8
  • 60
  • 136
  • 1
    Possible duplicate of [record/save audio from voice recognition intent](http://stackoverflow.com/questions/23047433/record-save-audio-from-voice-recognition-intent) – Nikolay Shmyrev May 15 '16 at 07:02

2 Answers2

0

I managed to solve the question.

The easiest way would be to use functions from apache commons IO

audioPath = uri.getText().toString();
                finalPath = combinedPath.replaceAll(" ","");
                Log.d("Filepath",combinedPath.replaceAll(" ",""));
                ContentResolver contentResolver = getContentResolver();
                try{
                    InputStream filestream = contentResolver.openInputStream(audioUri);
                    File targetFIle = new File(finalPath);
                    FileUtils.copyInputStreamToFile(filestream,targetFIle);
                }catch (IOException e){
                    e.toString();
                }
JianYA
  • 2,750
  • 8
  • 60
  • 136
-1
Button_to_speak.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            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, "Speak to text");

            try {
                startActivityForResult(intent, REQUEST_CODE_SPEECH_INPUT);
            }
            catch (Exception e) {
                Toast
                        .makeText(MainActivity.this, " " + e.getMessage(),
                                Toast.LENGTH_SHORT)
                        .show();
            }
        }
    });