8

I have looked almost everywhere trying to find a way to save my android tts output in an audio file. I looked at these posts:

but couldn't find/understand the answers. I am using synthesizeToFile() like this:

HashMap<String, String> myHashRender = new HashMap<String, String>();
myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, result);
String fileName = "/Android/data/com.android.voicelanglearning.vll/ttsfile1.wav";
tts.synthesizeToFile(result, myHashRender, fileName);

So I am reposting the same question. Any help is much appreciated.

Thank you, Mounika

Community
  • 1
  • 1
mnc
  • 93
  • 1
  • 5

3 Answers3

4

The important method is synthesizeToFile. It will write the audio to a file on the device that you specify. You can then play that file with a MediaPlayer or you can pull it off the device onto your development system with the adb command-line tool using the command

adb pull <path-to-file>
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • 1
    Thanks for the reply. But I cannot find the file after running synthesizeToFile(). I get the output as SUCCESS but I cannot see the file in the directory it supposed to save. – mnc Mar 15 '12 at 03:45
  • @MounikaNamburu - I think the problem is the file name you are using. You need to write to a folder where your app has write permission. Try using the folder returned by your activity's `getExternalFilesDir()` or else `Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)`. You also need WRITE_EXTERNAL_STORAGE permission declared in the manifest. – Ted Hopp Mar 15 '12 at 03:58
0

It might not be that it doesn't work on real devices. The problem could be that you are testing a real device with the USB cable plugged into the computer in debug mode. This might be disabling saving files to the phone.

Environment.getExternalStorageDirectory()

Return the primary external storage directory. This directory may not currently be accessible if it has been mounted by the user on their computer, has been removed from the device, or some other problem has happened. You can determine its current state with getExternalStorageState().

WoodsLink
  • 107
  • 5
-2

TextToSpeech.synthesizeToFile() doesn't work in real devices. It only works in AVDs.

I have been experimenting with this, too, using both the original Pico TTS engine and a third party TTS engine, trying to write to either the sdcard or the internal memory (on a rooted device):

context.getDir("soundfiles", Context.MODE_WORLD_WRITEABLE);

But, as you noted, the method returns TextToSpeech.SUCCESS without actually creating the file.

If you must record your TTS output to a WAV file, connect the headset output to the aux input in a sound card in your PC and use any recording software to capture that.

ateiob
  • 9,016
  • 10
  • 44
  • 55
  • 2
    TextToSpeech.synthesizeToFile() is working on my device (Nexus 4 running OS 4.2.2). It is synthesizing the specified text to a .wav file to my application's /sdcard/Android/data/ directory. – gregS Apr 11 '13 at 19:41