问题
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:
- How can I save TTS output in an audio file on android?
- How can I allow the TTS to write files to my app's directories?
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
回答1:
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>
回答2:
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().
回答3:
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.
来源:https://stackoverflow.com/questions/9713594/how-can-i-save-my-android-tts-output-in-a-wav-file