-1

i have successfully written and executed code for text to speech. the code is able to speak out the text written in edittext. but now i dont know how to convert it into .wav file and save it into directory. as i am writing this code for api 15+, plz suggest me something.

my code:

@Override
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {

            int result = tts.setLanguage(Locale.US);

            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "This Language is not supported");
            } else {
              //  btnSpeak.setEnabled(true);
                speakOut();
            }

        } else {
            Log.e("TTS", "Initilization Failed!");
        }
    }

    private void speakOut() {

        String text = txtText.getText().toString();

        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }

please some one help me...

av development
  • 53
  • 1
  • 10
  • 1
    Possible duplicate of [How can I save my android tts output in a wav file?](http://stackoverflow.com/questions/9713594/how-can-i-save-my-android-tts-output-in-a-wav-file) – Nikolay Shmyrev Apr 30 '17 at 19:53

1 Answers1

1

You can use the method synthetizeToFile(). You have to create a File with FileProvider to avoid restricted files above api 24. You have also to pass a request id.

String utteranceId = "MyUtteranceId"
File file = /* file obtained with FileProvider */
int result = tts.synthesizeToFile(text, null, file, utteranceId);
if (result == TextToSpeech.SUCCESS) {
    // It was successfull.
} else {
    // Error occurred
}

This method returns an int representing the result (error or success). I recommend you to move the synthetize on a background thread and dispatch the result on the UI thread after.

Take a look also to the File rendering and playback section of: https://android-developers.googleblog.com/2009/09/introduction-to-text-to-speech-in.html

Giorgio Antonioli
  • 15,771
  • 10
  • 45
  • 70