2

I want to write output from the TextToSpeech engine to my app's cache directory. In order to for the TTS to write there I first have to give it permissions to do so. But I don't know how. I understand that normally such problems can be solved by handing a FileDescriptor over thus giving permissions to access a specific file. But I can't do that with TTS, as the TextToSpeech.synthesizeToFile method only accepts the file path as a String, no FileDescriptor. So what to do?

To make my point that TTS really hasn't permissions to write to my app's directories, here's the code...:

TextToSpeech mTts = new new TextToSpeech(context, this);
mTts.synthesizeToFile(text, null, getCacheDir() + "/" + "speech.wav");

And the debugger log:

08-20 14:46:11.257: ERROR/TtsService(336): Can't create
/data/data/com.myorg.myapp/cache/speech.wav due to exception java.io.IOException: Permission denied
Arnulf
  • 48
  • 1
  • 7
  • 1
    [This thread](http://groups.google.com/group/android-developers/browse_thread/thread/79c0c4d9399b38ac) suggests that `TextToSpeech.synthesizeToFile()` only works on AVDs, not real phones... This is weird. I will investigate further to see whether this is indeed the case. – an00b Oct 13 '11 at 03:58
  • 1
    And [this thread](http://groups.google.com/group/android-developers/browse_thread/thread/d1365a29214b8cb3?fwc=1) suggests that it is possible to use `TextToSpeech.synthesizeToFile()` on a real device. But how??? – an00b Oct 13 '11 at 04:01
  • 2
    This is insane: I managed to no longer get the `java.io.IOException` and instead receive the SUCCESS message `Synthesizing to /mnt/sdcard/test.wav` but that test.wav is nowhere to be found anywhere on the file system (and definitely not on /mnt/sdcard). What's going on? – an00b Oct 13 '11 at 04:34

2 Answers2

6

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
-1

TextToSpeech.synthesizeToFile() does actually work on real devices, though it's a bit screwy. I was having the same issue but eventually got it working. The code I use is:

speakTextTxt = speakText.getText().toString();
HashMap<String, String> myHashRender = new HashMap<String, String>();
myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, UTTERANCE_SAVE_ID);

String exStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
File appTmpPath = new File(exStoragePath + "/sounds/");
appTmpPath.mkdirs();
String tempFilename = "tmpaudio.wav";
tempDestFile = appTmpPath.getAbsolutePath() + "/" + tempFilename;

mTts.synthesizeToFile(speakTextTxt, myHashRender, tempDestFile);

However, today I found something else quite strange. I switched to a different computer to do development on and then started getting the same error again "java.io.IOException: Permission denied". I then went into the Android SDK Manager and made sure all "SDK Platform" and "Google APIs by Goole Inc." packages were installed for all Android versions. I'm not sure exactly which package version fixed it but it's working again. So apparently you can have perfectly fine code but something might be off with your development environment that can cause issues. I hope this can help someone.

Trees
  • 117
  • 1
  • 9
  • Just came across another point related to this. This error also comes up if I have "USB Mass Storage" selected as the USB connection on the device. When any of the other options are selected it works fine. I believe this is due to the computer mounting the device as a usb drive and it becomes inaccessible to anything but the computer at that point. – Trees Jan 16 '12 at 22:32
  • 1
    On which device (brand & model, rooted?) did you have this working? +1 for now, although I tested this on 5 different Android phones and none of them resulted in a saved file. – ateiob Feb 28 '12 at 23:57
  • I have it working on at least a Motorola Atrix, Motorola Xoom, and Samsung Moment. – Trees Feb 29 '12 at 23:51
  • Also, the devices were not rooted. – Trees Mar 01 '12 at 02:19
  • I tried this on Android 2.x devices. Which versions of Android do your working devices run? – ateiob Apr 04 '12 at 00:10
  • I am not able to see the .wav file what ever I do.Can anyone help me with reference to this? – Sreekanth Karumanaghat Oct 18 '12 at 12:22