0

I have a RecyclerView with some video elements and they get restarted every time they get off-screen. I tried:

recyclerView.getRecycledViewPool().setMaxRecycledViews(RecyclerViewAdapter.TYPE_VIDEO, 0);

but no success. I also tried to do:

holder.setIsRecyclable(false)

inside my adapter, but videos still restart every time.

Is there any way to stop restarting videos, and somehow pause them and resume them once they are on screen again?

The videos are remote, not local. And I am using a class extending TextureView, not the Android's VideoView

moyo
  • 1,312
  • 1
  • 13
  • 29

1 Answers1

0

Edit: I missunderstood the question. See original answer below.

I guess you have two possibilities:

  1. Ugly, only possible if the list is not going to be too long™ and undermining the purpose of the recyclerview: use setItemViewCacheSize(int) to increase the number of viewholders that are cached to the number of videos you have and just pause and play during attach / detach.
  2. much better: Store and restore the current playback timestamp of each video when the viewholder gets attached/dettached.

You can use an RecyclerView.OnChildAttachStateChangeListener that will be called whenever a view is attached / removed. Do it like this:

mRecyclerView.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
    @Override
    public void onChildViewAttachedToWindow(View view) {
        YourTextureView yourTextureView = (YourTextureView) view.findViewById(R.id.yourvideoviewid);
        if (yourTextureView != null) {
            // start your video
        }
    }

    @Override
    public void onChildViewDetachedFromWindow(View view) {
        YourTextureView yourTextureView = (YourTextureView) view.findViewById(R.id.yourvideoviewid);
        if (yourTextureView != null) {
            // stop your video
        }
    }
});
wedi
  • 1,332
  • 1
  • 13
  • 28