Release ExoPlayer in RecyclerView

后端 未结 3 1694
被撕碎了的回忆
被撕碎了的回忆 2021-02-10 01:51

I am implementing the ExoPlayer in a RecyclerView. But while scrolling the Video stops, but not the audio.

How can I release the ExoPlayer in the RecyclerView? Or how c

相关标签:
3条回答
  • 2021-02-10 02:37

    I think that it would be cleanest to release the player when its view is recycled.

    Adapter Class:

    @Override
    public void onViewRecycled(@NonNull ViewHolder holder) {
        int position = holder.getAdapterPosition();
        if (mDataset.get(position) != null) {
            mDataset.get(position).getPlayer().release();
        }
        super.onViewRecycled(holder);
    }
    
    0 讨论(0)
  • 2021-02-10 02:39
      holder.yourplayername.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
                @Override
                public void onViewAttachedToWindow(View v) {
    
                }
    
                @Override
                public void onViewDetachedFromWindow(View v) {
                    holder.yourplayername.getPlayer().stop();
    
                }
            });
    

    hi.i hope this code helpful for you becuse work for me.

    0 讨论(0)
  • 2021-02-10 02:53

    The system cannot hold multiple ExoPlayer instances. It can throw an OOM Exception. Therefore, you must release ExoPlayer when the user does not see the item. In this case, I suggest you use the bellow method from RecyclerView.Adapter

    override fun onViewDetachedFromWindow(holder: ViewHolder) {
        holder.itemView.player?.stop()
        holder.itemView.player?.release()
        holder.itemView.player = null
        super.onViewDetachedFromWindow(holder)
    }
    

    And then, in ViewHolder class you should check, if player == null, you must initialize it.

    0 讨论(0)
提交回复
热议问题