Unable to Show imageView in recyclerVIew synchronously with TTS

前端 未结 3 1439
面向向阳花
面向向阳花 2020-12-11 09:44

How can i show/Hide imageView in recyclerView as TTS(text to speech) plays for all the available list items, one by one!

Activity method -This met

3条回答
  •  佛祖请我去吃肉
    2020-12-11 10:16

    Please check this out. In your code, you are missed UtteranceProgressListener. You have to add UtteranceProgressListener which will give you speech completion listener events. Also, you will need utteranceID which will be passed to tts.speak() that helps you to identify the speech if you need it. Take a variable as,

    String utterId = "";
    

    then in your TextToSpeech onInit before calling convertTextToSpeech register this listener to your tts.

    ...
    else{
     tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
                                        @Override
                                        public void onStart(String utteranceId) {
    
                                        }
    
                                        @Override
                                        public void onDone(String utteranceId) {
                                            convertTextToSpeech(position);
                                        }
    
                                        @Override
                                        public void onError(String utteranceId) {
    
                                        }
                                    });
                                    convertTextToSpeech(0);
    }
    

    Initially, 0 is passed to convertTextToSpeech as it is the first time call. Here I've made some changes to the convertTextToSpeech. In this, I've removed the loop you were using earlier instead, it will be a recursive function.

     public void convertTextToSpeech(int pos) {
            if (pos >= items.size()) return;
            position = pos;
            final Multiples multiples = items.get(position);
    
            text = multiples.first + "  " + multiples.getSecond() + " Za " + multiples.getResult() + ".";
    
            utterId = (new Random().nextInt() % 9999999) + ""; // "" is String force
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Bundle params = new Bundle();
                params.putString(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, utterId);
    
                tts.speak(text, TextToSpeech.QUEUE_FLUSH, params, utterId);
            } else {
                HashMap params = new HashMap<>();
                params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, utterId);
    
                tts.speak(text, TextToSpeech.QUEUE_FLUSH, params);
            }
    
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    MyAdapter m = (MyAdapter) mAdapter;
                    multiples.setImage_show(true);
                    m.updateItem(multiples, position);
                    position++;
                }
            });
    
        }
    

    As you can see the tts.speak() is deprecated you should use the new one to avoid issues in the latest android os.

    In your MyAdapter, you have to set previous item image_show to false.

    public void updateItem(Multiples newItem, int pos) {
        if (pos > 0) {
            items.get(pos - 1).setImage_show(false);
        }
        Log.i("values", "-----In updateItem Log----Row value-" + newItem.getFirst() + " X " + newItem.getSecond() + ", Position=" + pos);
        items.set(pos, newItem); //update passed value in your adapter's data structure
        Log.e("msg", "-----items.get(pos)------------->" + items.get(pos).getFirst() + " X " + items.get(pos).getSecond());
    
        notifyDataSetChanged();
    }
    

    Check the gif uploaded. Please keep us updated.

    If you need any help you can ask. Thanks

提交回复
热议问题