Search code examples
javaandroidspeech-to-textgoogle-speech-api

Android save Speech to Text audio


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.


Solution

  • 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();
                    }