How can I save my android tts output in a wav file

2019-02-09 08:19发布

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

3条回答
迷人小祖宗
2楼-- · 2019-02-09 08:32

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.

查看更多
看我几分像从前
3楼-- · 2019-02-09 08:36

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().

查看更多
Fickle 薄情
4楼-- · 2019-02-09 08:40

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>
查看更多
登录 后发表回答