2017-07-26 45 views
0

我試圖將字符串轉換爲.wav文件,以便在texttospeech中使用暫停/恢復功能,但是我無法創建文件。synthesizeToFile返回-1,我無法使用Environment.getExternalStorageDirectory()創建文件getAbsolutePath()

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

回答

0

我已經看到您使用的已棄用方法接收3個參數。你是否已經嘗試過使用synthesizeToFile(CharSequence文本,Bundle params,File file,String utteranceId)?

另外,您是否在清單中添加了權限?

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

您也應該檢查文件是否在任一聽衆onUtteranceCompletedListener或UtteranceProgressListener存在,因爲該方法synthesizeToFile是異步的。

+0

是的我已經在應用程序標記上方的清單文件中給予權限,但是在logcat監視器中它顯示java.io.FileNotFoundException:打開失敗:EACCES(Permission denied)@cristianorbs – kunal007

相關問題