synthesizeToFile返回-1,我无法使用Environment.getExternalStorageDirectory()创建文件.getAbsolutePath()

时间:2017-07-26 17:03:14

标签: android

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

    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());
}

1 个答案:

答案 0 :(得分:0)

我已经看到你正在使用收到3个参数的弃用方法。你有没有尝试过使用synthesizeToFile(CharSequence text,Bundle params,File file,String utteranceId)?

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

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

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