0

I need to start a drawable animation when my text to speech is starting and stop this one when the text to speech is over, but i can't stop the animation.

Code:

tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {

      @Override
        public void onInit(int status) {
            if (status == TextToSpeech.SUCCESS) {
                int result = tts.setLanguage(Locale.US);

                if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                    Log.e("TTS", "This Language is not supported");
                }

            } else {
                Log.e("TTS", "Initilization Failed!");
            }
        }
    });

private void speak(String text){
    animation.start();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, null);

    }else{
        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);

    }
}

and here my animationdrawable xml

 <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/selected" android:oneshot="false" >

<item android:drawable="@drawable/face_1a_mini" android:duration="250" />

<item android:drawable="@drawable/face_1b_mini" android:duration="250" />
<item android:drawable="@drawable/face_1c_mini" android:duration="250" />

<item android:drawable="@drawable/face_1d_mini" android:duration="250" />
 </animation-list>
eouw0o83hf
  • 9,438
  • 5
  • 53
  • 75
  • 3
    Hi and welcome to Stack Overflow, please take a time to go through the [welcome tour](https://stackoverflow.com/tour) to know your way around here (and also to earn your first badge), read how to create a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and also check [How to Ask Good Questions](https://stackoverflow.com/help/how-to-ask) so you increase your chances to get feedback and useful answers. – DarkCygnus Jun 28 '17 at 17:57
  • Possible duplicate of [How to know when TTS is finished?](https://stackoverflow.com/questions/4658376/how-to-know-when-tts-is-finished) – Nikolay Shmyrev Jun 29 '17 at 09:25

3 Answers3

2

You have to add the Utterance Id to the speak method

tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {

    @Override
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            int result = tts.setLanguage(Locale.US);

            if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "This Language is not supported");
            }
            textToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
                @Override
                public void onStart(String utteranceId) {
                    Log.i("TextToSpeech","On Start");
                    animation.start();
                }

                @Override
                public void onDone(String utteranceId) {
                    Log.i("TextToSpeech","On Done");
                    animation.stop();
                }

                @Override
                public void onError(String utteranceId) {

                }
            });

        } else {
            Log.e("TTS", "Initilization Failed!");
        }
    }
});

private void speak(String text){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, TextToSpeech.ACTION_TTS_QUEUE_PROCESSING_COMPLETED);

    }
}
kelvin andre
  • 395
  • 5
  • 11
0

You must wrap it with while(tts.isSpeaking())

example :

while(tts.isSpeaking())
            {
                Animation animation1 = 
                AnimationUtils.loadAnimation(this,R.anim.fadein);
                view.startAnimation(animation1);
            }
Anandhu Nadesh
  • 672
  • 2
  • 11
  • 20
0

start your animation on start of utterance and stop when utterance is done.

t1 = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
                @Override
                public void onInit(int status) {
                    if (status != TextToSpeech.ERROR) {
                        t1.setLanguage(Locale.UK);
                    t1.setOnUtteranceProgressListener(new UtteranceProgressListener() {
                        @Override
                        public void onStart(String utteranceId) {

                        }

                        @Override
                        public void onDone(String utteranceId) {
                            if(utteranceId.equals("finish")){
                                finish();
                            }
                        }

                        @Override
                        public void onError(String utteranceId) {

                        }
                    });
                        }

                }
            });
Durgesh Kumar
  • 935
  • 10
  • 17