当文本到语音结束时做一些事情

时间:2017-06-28 17:18:42

标签: animation drawable text-to-speech animationdrawable

我需要在我的文本到语音开始时启动一个可绘制的动画,并在文本到语音结束时停止这个动画,但我无法停止动画。

代码:

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

    }
}

这里是我的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>

3 个答案:

答案 0 :(得分:1)

您必须将语音ID添加到语音方法

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

    }
}

答案 1 :(得分:0)

您必须使用while(tts.isSpeaking())

将其换行

示例:

while(tts.isSpeaking())
            {
                Animation animation1 = 
                AnimationUtils.loadAnimation(this,R.anim.fadein);
                view.startAnimation(animation1);
            }

答案 2 :(得分:0)

在话语开始时开始动画,在话语开始时停止。

 if (Picturebox1 != null && Picturebox1.Image != null)
 {
       Image img = Picturebox1.Image;
       Picturebox1.Image = null;
       img.Dispose();
 }