Java 播放TextToSpeech时突出显示文本

Java 播放TextToSpeech时突出显示文本,java,android,text-to-speech,Java,Android,Text To Speech,我在屏幕上显示文本,在我的代码中,我试图在TextToSpeech开始说话之前突出显示文本,并在它结束时将颜色更改回原来的颜色。然而,它从不改变颜色为蓝色,或者至少看起来是这样 这就是TextToSpeech的起点 ImageView playButton = (ImageView) blankPage.findViewById(R.id.play_button); playButton.setOnClickListener(new View.OnClickListener()

我在屏幕上显示文本,在我的代码中,我试图在TextToSpeech开始说话之前突出显示文本,并在它结束时将颜色更改回原来的颜色。然而,它从不改变颜色为蓝色,或者至少看起来是这样

这就是TextToSpeech的起点

ImageView playButton = (ImageView) blankPage.findViewById(R.id.play_button);
    playButton.setOnClickListener(new View.OnClickListener()

                                  {
                                      @Override
                                      public void onClick(View v) {
                                          findHighlightedWords();


                                          mTts.setSpeechRate(0.60f);
                                          play(mWordsToSpeechBank, mHighlightedTextViews);
   }
}
这就是玩法

@SuppressWarnings("deprecation")

private void play (ArrayList<String> playMe, ArrayList<TextView> highlightedWords){

    if (playMe.size() > 0) {
        final ArrayList<String> wordsToPlay = new ArrayList<String>(playMe);
        final ArrayList<TextView> highLightedTextView = new ArrayList<TextView>(highlightedWords);

        final TextView textView = highLightedTextView.get(0);

        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                textView.setBackgroundColor(Color.BLUE);
            }
        });

        String getFirst = wordsToPlay.get(0);
        wordsToPlay.remove(0);
        highLightedTextView.remove(0);

        mTts.speak(getFirst, TextToSpeech.QUEUE_FLUSH, null);

        do {

        } while (mTts.isSpeaking());

        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                textView.setBackgroundColor(Color.YELLOW);
            }
        });
        play(wordsToPlay, mHighlightedTextViews);
    }
}
@SuppressWarnings(“弃用”)
私人虚空游戏(ArrayList playMe,ArrayList highlightedWords){
如果(playMe.size()>0){
最终ArrayList wordsToPlay=新ArrayList(playMe);
最终ArrayList highLightedTextView=新ArrayList(highlightedWords);
final TextView TextView=highLightedTextView.get(0);
getActivity().runOnUiThread(新的Runnable()){
@凌驾
公开募捐{
textView.setBackgroundColor(Color.BLUE);
}
});
字符串getFirst=wordsToPlay.get(0);
wordsToPlay.remove(0);
highLightedTextView。删除(0);
mTts.speak(getFirst,TextToSpeech.QUEUE\u FLUSH,null);
做{
}而(mTts.isSpeaking());
getActivity().runOnUiThread(新的Runnable()){
@凌驾
公开募捐{
textView.setBackgroundColor(颜色:黄色);
}
});
播放(文字停止播放,mHighlightedTextView);
}
}
当我看日志时,上面写着
“编舞﹕ 跳过206帧!应用程序可能在其主线程上做了太多工作。“

单击UI线程上运行的处理程序,然后单击
while(mTts.isSpeaking())循环导致应用程序挂起UI线程,直到TTS结束讲话。TextToSpeech.speak()方法是异步的——您应该使用回调来确定语音是否已完成。谢谢!!这很管用,而且很有意义!您还可以使用高亮显示单个TextView中的区域,而不是创建多个TextView。我在中使用setSelection(),因为我使用的是EditText,但想法类似。单击UI线程上运行的处理程序,然后单击您的
while(mTts.isSpeaking())循环导致应用程序挂起UI线程,直到TTS结束讲话。TextToSpeech.speak()方法是异步的——您应该使用回调来确定语音是否已完成。谢谢!!这很管用,而且很有意义!您还可以使用高亮显示单个TextView中的区域,而不是创建多个TextView。我在中使用setSelection(),因为我使用的是EditText,但想法类似。