OBX
OBX

Reputation: 6114

Text To Speech in Dialog, weird behavior

The code opens a Dialog , in which a user entered EditText is converted to speech using TTS and the output is played.

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

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


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

            String destFileName = Environment.getExternalStorageDirectory()+"/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();
}

And obviously there are two buttons in the Dialog, setPositiveButton and setnegativeButton , positive button saves the output as a .wav file , also plays the output. the negative button also plays the output. But t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null); does not produce any output in the positive button. What is causing this? Anything to take care while using Dialogs?

Upvotes: 0

Views: 276

Answers (1)

Eborbob
Eborbob

Reputation: 1975

It's nothing to do with Dialogs, the problem is you are telling it to stop!

The call to speak() is non-blocking. From the API:

The synthesis might not have finished (or even started!) at the time when this method returns.

Your code is:

START SPEAKING -> t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);

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

                  myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, toSpeak);
                  t1.synthesizeToFile(toSpeak, myHashRender, destFileName);
STOP SPEAKING ->  t1.stop();

So after you tell it to start you do a few more things, then tell it to stop - most likely before any sound has even been produced.

Upvotes: 1

Related Questions