Skip to content Skip to sidebar Skip to footer

Unable To Show Imageview In Recyclerview Synchronously With Tts

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 method is called r with Loop(not wo

Solution 1:

You main fail is absence of listener for TTS. Without it you don't know when you should update your RecyclerView. Listener can looks like this:

classMyListenerextendsUtteranceProgressListener {
        @OverridepublicvoidonStart(String utteranceId) {
            int currentIndex = Integer.parseInt(utteranceId);
            mMainAdapter.setCurrentPosition(currentIndex);
            handler.post(newRunnable() {
                @Overridepublicvoidrun() {
                    mMainAdapter.notifyDataSetChanged();
                }
            });
        }

        @OverridepublicvoidonDone(String utteranceId) {
            int currentIndex = Integer.parseInt(utteranceId);
            mMainAdapter.setCurrentPosition(-1);
            handler.post(newRunnable() {
                @Overridepublicvoidrun() {
                    mMainAdapter.notifyDataSetChanged();
                }
            });
            if (currentIndex < data.size() - 1) {
                playSound(currentIndex + 1);
            }
        }

        @OverridepublicvoidonError(String utteranceId) {
        }
    }

I've created a test project to show how it can be implemented. Here you can see how it works. Here is my github repository.

Solution 2:

Put the below code snippet inside your Adapter in OnBindViewHolder

        text = item.first + "  " + item.getSecond() + " Za " + item.getResult() + ".";
        tts.speak(text, TextToSpeech.QUEUE_ADD, null);
        booleanspeakingEnd= tts.isSpeaking();


        if(speakingEnd){
            Toast.makeText(getApplicationContext(), "Speaking...."+position, Toast.LENGTH_SHORT).show();
            multiples1.setImage_show(true);
            mAdapter.notifyItemChanged(position);

        } else {
            Toast.makeText(getApplicationContext(), "Done...."+position, Toast.LENGTH_SHORT).show();
        }

May this help Thanks.

Solution 3:

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,

StringutterId="";

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

...
else{
 tts.setOnUtteranceProgressListener(newUtteranceProgressListener() {
                                    @OverridepublicvoidonStart(String utteranceId) {

                                    }

                                    @OverridepublicvoidonDone(String utteranceId) {
                                        convertTextToSpeech(position);
                                    }

                                    @OverridepublicvoidonError(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.

publicvoidconvertTextToSpeech(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 forceif (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<String, String> params = new HashMap<>();
            params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, utterId);

            tts.speak(text, TextToSpeech.QUEUE_FLUSH, params);
        }

        runOnUiThread(new Runnable() {
            @Override
            publicvoidrun() {
                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.

publicvoidupdateItem(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.

enter image description here

If you need any help you can ask. Thanks

Post a Comment for "Unable To Show Imageview In Recyclerview Synchronously With Tts"