TextToSpeech delays in starting

0

When starting a Activity I execute the following code in the function onCreate :

tts = new TextToSpeech(this, new TextToSpeech.OnInitListener()
{
    @Override
    public void onInit(int status)
    {
        if (status == TextToSpeech.SUCCESS)
        {
            tts.setLanguage(singletonGame.locale);
            tts.speak(instruction, TextToSpeech.QUEUE_ADD, null);
        }
    }
});

But there is a delay in when the audio starts to sound, because it is delayed.

Try to leave the object TextToSpeech in a class Singleton to have it pre-loaded before and not wait for the delay, but throw me an error, because I must destroy it in each scene that the object calls.

Does anyone know any solution?

Update 1:

I did a test and what is delayed is this line running, because it goes fast to IF

tts.speak(instruction, TextToSpeech.QUEUE_ADD, null);

    
asked by albertcito 02.06.2017 в 19:04
source

1 answer

0

It is delayed because the function speak has a run and waits for a service, that's why.

So the solution I found is to post a message that says "Loading ..."

tts = new TextToSpeech(this, new TextToSpeech.OnInitListener()
{
    @Override
    public void onInit(int status)
    {
        if (status == TextToSpeech.SUCCESS)
        {
            tts.setLanguage(singletonGame.locale);
            tts.speak(cs, TextToSpeech.QUEUE_FLUSH, null,"idUnico");
        }
    }
});

And I add the functions when it starts and ends:

tts.setOnUtteranceProgressListener(new UtteranceProgressListener()
{
    @Override
    public void onStart (String utteranceId)
    {
        Log.d("tts_","empezando");
    }

    @Override
    public void onError (String utteranceId){ Log.d("tts_","error"); }

    @Override
    public void onDone (String utteranceId)
    {
        Log.d("tts_","listo");
    }

});
    
answered by 08.06.2017 / 18:49
source