OBX
OBX

Reputation: 6114

Text to speech from Dialog

Trying to save the output of a TTS as a .wav file. However, having trouble saving the output as I am not familiar with file structure. Moreover, I am testing it on a Nexus 5 , which does not support SD storage.

When I open the Dialog and try to save the file, it crashes the entire app. The error is the directory does not exists. Here is the code I used :

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

Here is the logcat as well:

> 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)

I tried using this question as well to create a directory, but it does not work. Any help would be appreciated.

Upvotes: 2

Views: 288

Answers (1)

pri
pri

Reputation: 1531

Try using one or more of the following:

1) Make sure you have WRITE_EXTERNAL_STORAGE permission in your AndroidManifest.xml

2) Use mkdirs() function, this will create the parent directories if they do not exist.

3) Use Environment.getExternalStorageDirectory() instead of Environment.getDataDirectory() and try writing it in external storage.

Even if your device does not support external SD card, some part of the internal memory may be exposed as external storage. Check this link:
source.android.com/devices/storage

Upvotes: 1

Related Questions