I need to create an audio file with synthesizeToFile.
It works on Android 6 (with the overloaded version of synthesizeToFile) but in Android 4.1 synthesizeToFile
returns -1
.
The synthesizeToFile
official documentation says:
Synthesizes the given text to a file using the specified parameters. This method is asynchronous, i.e. the method just adds the request to the queue of TTS requests and then returns.
Then, to know which error caused that -1
I searched in the logcat where I founded this exception:
E/TextToSpeechService: Can't use /data/data/com.domain.my/files/_12345_test.wav due to exception java.io.IOException: open failed: EACCES (Permission denied)
There is some different system configuration/setting between Android 6 and 4.1 which cause this error?
I must pass to synthesizeToFile
a different path than the one returned by getFilesDir()
?
I must set file permissions?
Code I used for Android 4.1:
TextToSpeech tts = new TextToSpeech(getApplicationContext(), this, "com.google.android.tts");
public void onInit(int status)
{
if (status == TextToSpeech.SUCCESS)
{
String textToGenerate = "this is a test";
// /data/data/com.domain.my/files is returned by getFilesDir()
String completePathFile = "/data/data/com.domain.my/files/_12345_test.wav";
File fileToGenerate = new File(completePathFile);
String fileName = fileToGenerate.getName();
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, fileName);
int response = tts.synthesizeToFile
(
textToGenerate
, hashMap
, completePathFile
);
Log.d("testTTS", "Generation file " + fileName + " - response = " + response);
}
}
I already checked with getEngines() that "com.google.android.tts" is installed.
I need to save the file in the internal storage so I must not ask for external storage permission (it is true also for Android 4.1? Or for this versione I need to do so?).
If I deliberately pass to synthesizeToFile
a path that doesn't exists, the error in the logcat changes to file not found exception
so that method checks correctly that the path completePathFile
exists.