Pregunta

I cannot figure out why TTS does not work consistently. I would like some insight on how to improve my current setup. The application begins with TTS, welcoming the user or prompting the user to say a voice command. So in onCreate I simply have the following:

textToSpeech = new TextToSpeech(TTSActivity.this, this); 

My TTSActivity class implemetns OnInitListener, so I also have the following override method:

@Override
    public void onInit(int status) {

        if (status == TextToSpeech.SUCCESS) {
            textToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {

                @Override
                public void onStart(String utteranceId) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onError(String utteranceId) {               
                    // TODO Auto-generated method stub

                }

                @Override
                public void onDone(String utteranceId) {
                    //do some work here
                }
            });

            textToSpeech.setLanguage(Locale.US);
            map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "UniqueID");

            ttsString(0);

        } else if (status == TextToSpeech.ERROR){
            //try to restart TTS
            textToSpeech = new TextToSpeech(TTSActivity.this, this);
            ttsString(0);
        }
    } //end onInit() method

Some explanations: The textToSpeech.setOnUtteranceProgressListener() is there so that I can activate voice recognition once TTS engine has stopped speaking. map is from HashMap map = new HashMap() and ttsString are all the strings I want the TTS to speak. So my ttsString(id#) method looks like the following. Before I make TTS speak, I call ttsString(id#) to set the string.

private void ttsString(int id)
    {   

        switch (id)
        {
            case 0:{
                metaString = "Say string one";
                    speakText(metaString);
                    break;
            }
            case 1:{
                    metaString = "Say string two";
                    speakText(metaString);
                    break;
            }
            case 2:{
                    metaString = "Say string three";
                    speakText(metaString);
                    break;
            }

        }
    }

This is how I get TTS to speak:

private void speakText(String text) {
        if(textToSpeech.isSpeaking()) {
            return;
        } else {
            textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, map);
        } 
    }

I am not receiving TTS unbound errors, everything works at one point in time, but not consistently. In other words, sometimes the program will speak and sometimes it will not. What do you suggest I do? Re-structure?

¿Fue útil?

Solución

few hints:

In your if (status == TextToSpeech.ERROR), calling ttsString(0); will always fail, because TTS is not yet initialized.

You seem to not call tts.shutdown(). Actually I would initialize tts in onStart(), and call shutdown() in onStop().

In my app I have put TTS into service, so to not bother with all the initialization/deinitialization while my activity/fragment is recreated during config changes.

Look into logcat, you should see there any errors. I have seen strange errors when using svox voices, as far as I remember the problem was from not calling properly shutdown.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top