synthesizeToFile 返回 -1,我无法使用 Environment.getExternalStorageD



我正在尝试将字符串转换为.wav文件,将暂停/恢复功能转换为文本语音,但我无法创建该文件。

public void fileCreate() {
String inputText = editText.getText().toString();
HashMap<String, String> myHashRender = new HashMap<String, String>();
myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, inputText);
Log.d("TAG", "successfully created hashmap");
int sr = textToSpeech.synthesizeToFile(inputText, myHashRender,      Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"tts_file.wav");
Log.d("TAG", "synthesize returns = " + sr);
File fileTTS = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath(),"tts_file.wav");
if (fileTTS.exists()) {
Log.d("TAG", "successfully created fileTTS");
}
else {
Log.d("TAG", "failed while creating fileTTS");
}
fileUri = Uri.fromFile(fileTTS);
Log.d("TAG", "successfully created uri link: " + fileUri.getPath());
}

我已经看到您正在使用接收 3 个参数的已弃用方法。您是否已经尝试使用synthesizeToFile(CharSequence文本,捆绑参数,文件文件,字符串话语Id(?

另外,您是否在清单中添加了权限?

<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

您还应该检查 UtteranceCompleteListener 或 UtteranceProgressListener 上的侦听器中是否存在文件,因为该方法 synthesizeToFile 是异步的。

最新更新