2015-07-05 62 views
2

試圖將TTS的輸出保存爲.wav文件。但是,由於我不熟悉文件結構,因此無法保存輸出。而且,我正在Nexus 5上測試它,它不支持SD存儲。來自Dialog的文本到語音

當我打開對話框並嘗試保存文件時,它會崩潰整個應用程序。錯誤是目錄不存在。下面是我使用的代碼:

final EditText input = new EditText(this); 
    input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_CLASS_TEXT); 
    builder.setView(input); 


    builder.setPositiveButton("SAVE", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      //m_Text = input.getText().toString(); 


       File directory = new File(Environment.getDataDirectory() 
         + "/myAppCache/"); 
      if (!directory.exists()) { 
       directory.mkdir(); 
      } 





      HashMap<String, String> myHashRender = new HashMap(); 
      String toSpeak = input.getText().toString(); 
      t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null); 

      String destFileName = Environment.getDataDirectory()+"/myAppCache/wakeUp.wav"; 

      myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, toSpeak); 
      t1.synthesizeToFile(toSpeak, myHashRender, destFileName); 
      t1.stop(); 
      t1.shutdown(); 
     } 
    }); 
    builder.setNegativeButton("Play", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      /*String toSpeak = input.getText().toString(); 
      t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);*/ 



     } 
    }); 

    builder.show(); 
} 

這裏是logcat的還有:

> 07-05 11:34:48.824 2471-2471/zyia.alarm.zyia.zyia E/TextToSpeech﹕ Opening file /storage/emulated/0/myAppCache/wakeUp.wav failed 
    java.io.FileNotFoundException: No such file or directory 
      at android.os.Parcel.openFileDescriptor(Native Method) 
      at android.os.ParcelFileDescriptor.openInternal(ParcelFileDescriptor.java:253) 
      at android.os.ParcelFileDescriptor.open(ParcelFileDescriptor.java:199) 
      at android.speech.tts.TextToSpeech$17.run(TextToSpeech.java:1793) 
      at android.speech.tts.TextToSpeech$17.run(TextToSpeech.java:1783) 
      at android.speech.tts.TextToSpeech$Connection.runAction(TextToSpeech.java:2232) 
      at android.speech.tts.TextToSpeech.runAction(TextToSpeech.java:742) 
      at android.speech.tts.TextToSpeech.runAction(TextToSpeech.java:732) 
      at android.speech.tts.TextToSpeech.synthesizeToFile(TextToSpeech.java:1783) 
      at android.speech.tts.TextToSpeech.synthesizeToFile(TextToSpeech.java:1840) 
      at zyia.alarm.zyia.zyia.AddAlarm$7.onClick(AddAlarm.java:207) 
      at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:162) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5254) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

我使用this question以及創建一個目錄試過,但它不工作。任何幫助,將不勝感激。

回答

1

嘗試使用一個或多個以下:

1)請確保您有WRITE_EXTERNAL_STORAGE許可,您的AndroidManifest.xml

2)使用mkdirs()功能,這將創建父目錄,如果他們不這樣做存在。

3)使用Environment.getExternalStorageDirectory()而不是Environment.getDataDirectory()並嘗試將其寫入外部存儲。

即使您的設備不支持外部SD卡,內部存儲器的某些部分可能會暴露爲外部存儲器。檢查此鏈接:
source.android.com/devices/storage