我正在尝试使用synthesizeToFile 创建一个文件:
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";
File fileToGenerate = new File(completePathFile);
String fileName = fileToGenerate.getName();
// this works on Android 6
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
Bundle bundleTts = new Bundle();
bundleTts.putString(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, fileName);
tts.synthesizeToFile
(
textToGenerate
, bundleTts
, fileToGenerate
, fileName
);
}
// this doesn't works on Android 4.1: response is -1
else
{
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);
}
}
}
对于搭载 Android 6 的设备,synthesizeToFile
方法可以正常工作。
对于搭载 Android 4.1 的设备,synthesizeToFile
方法返回 -1
。
我已经使用 getEngines() 检查了“com.google.android.tts”是否已安装。
如何调试脚本以找出 synthesizeToFile
返回 -1
的原因?
还有另一种方法可以使用 TTS 生成该文件吗?
我需要在内部存储中执行此操作(getFilesDir()
返回的路径),因此我不能请求外部存储权限。
编辑:
在 logcat 中我发现了这个错误:
E/TextToSpeechService: Can't use /data/data/com.domain.my/files/_12345_test due to exception java.io.IOException: open failed: EACCES (Permission denied)
我已经尝试过:
setWritable(true)
和
setWritable(true, true)
但即使两者都返回true
,异常仍然会发生。
那么,现在如何解决这个问题?
最佳答案
我发现要了解 synthesizeToFile
返回值 -1
的原因,我需要在 logcat 中查看:
E/TextToSpeechService: Can't use /data/data/com.domain.my/files/_12345_test due to exception java.io.IOException: open failed: EACCES (Permission denied)
现在,我必须知道为什么会发生这个异常...
关于java - 为什么 TextToSpeech SynthesizeToFile 返回 -1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56806994/