首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在文本到语音转换结束时执行某些操作

在文本到语音转换结束时执行某些操作
EN

Stack Overflow用户
提问于 2017-06-29 01:18:42
回答 3查看 199关注 0票数 0

当文本到语音转换开始时,我需要开始一个可绘制的动画,当文本到语音转换结束时,我需要停止这个动画,但我不能停止动画。

代码:

代码语言:javascript
复制
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);

    }
}

这里是我的动画可绘制xml

代码语言:javascript
复制
 <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>
EN

Stack Overflow用户

发布于 2019-11-17 04:24:18

您必须将发言Id添加到speak方法中

代码语言:javascript
复制
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);

    }
}
票数 2
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44808908

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档