Get URI of saved audio in OnResults(Bundle result)

2019-01-26 19:43发布

问题:

I followed almost everylink for audio saving and converting it to text but I did not find any way that how I can get audio Url in custom Recognition Listener

  1. record/save audio from voice recognition intent

  2. How to use Androids' speech to text with audio sample file

and many others as welll.... Audio url that I got here with the help of RecognizerIntent

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case REQ_CODE_SPEECH_INPUT: {
                if (resultCode == getActivity().RESULT_OK && null != data) {

                    ArrayList<String> result = data
                            .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    txtSpeechInput.setText(result.get(0));
                    Uri audioUri = data.getData();
                    ContentResolver contentResolver = getActivity().getContentResolver();
                    try {
                        InputStream filestream = contentResolver.openInputStream(audioUri);
                        copyInputStreamToFile(filestream, new File(Environment.getExternalStorageDirectory()+ "/recording.amr"));
                        Log.e("audio", "uri= "+ audioUri);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                }

                break;
            }

        }
    }

I want to get it here because I dont want to use Google dialog I want to customize it without dialog

 @Override
    public void onResults(Bundle results) {
        String str = new String();
        Log.d("testing1", "onResults " + results);
        data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

        str=data.get(0)+"";
        result_txt.setText(str);
        speech.stopListening();

        if(speech!=null){
            speech.destroy();
        }
    }

But I am not able to get URL from this bundle results. I have two queries:

  • Please let me know Hoe can I achieve audio saved URI in bundles
  • When I Customized Speech to Text It listens for a long time Just like RecognitionIntent if audio is not recording in 2-3 seconds it says did nt find any sound Same thing I want to do it with customized one as it listens for a long time. I followed these links for second qestion:
  • Android - Speech Recognition Limiting Listening Time

  • Voice Recognition stops listening after a few seconds

and many others as well. Its not the duplicate question people have already asked this question but solution given to them is using

startActivityForResult

and then get the audio url in above mentioned snippet of code but i want to get it in

onResults(Bundle results)

Please ask me if any doubt is there. I want to use it in fragment. Thanks in advance!!.