MostafaAnter / VideoPlayerInsideRecyclerView

Integrate RecyclerView with ExoPlayer — The clean way — and customization
Apache License 2.0
127 stars 25 forks source link

Player stop working when we call notifydatasetchanged() #7

Open zakirhussain49 opened 3 years ago

zakirhussain49 commented 3 years ago

When I add data in recycleview and try to scroll on particular index , player not working .

mBinding.recyclerView.scrollToPosition(scrollPosition);

Please help me how i achieve the task

MostafaAnter commented 3 years ago

Share your code so i can check it

Faiyyazauxy commented 3 years ago

Hi, this is the code with notifyItemChanged of your InstagramRecyclerAdapter class

package app.videoplayerinsiderecyclerview.views.instagramScreen

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import app.videoplayerinsiderecyclerview.R
import app.videoplayerinsiderecyclerview.databinding.InstagramTimelineItemRecyclerBinding
import app.videoplayerinsiderecyclerview.models.MediaObject
import app.videoplayerinsiderecyclerview.utils.PlayerStateCallback
import app.videoplayerinsiderecyclerview.utils.PlayerViewAdapter
import com.google.android.exoplayer2.Player
import java.util.*

/**
 * A custom adapter to use with the RecyclerView widget.
 */
class InstagramRecyclerAdapter(
    private val mContext: Context,
    private var modelList: ArrayList<MediaObject>) : RecyclerView.Adapter<RecyclerView.ViewHolder>(),
    PlayerStateCallback {
    private var mItemClickListener: OnItemClickListener? = null

    fun updateList(modelList: ArrayList<MediaObject>) {
        this.modelList = modelList
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(
        viewGroup: ViewGroup,
        viewType: Int
    ): VideoPlayerViewHolder {
        val binding: InstagramTimelineItemRecyclerBinding = DataBindingUtil.inflate(LayoutInflater.from(viewGroup.context)
            , R.layout.instagram_timeline_item_recycler, viewGroup, false)
        return VideoPlayerViewHolder(binding)
    }

    override fun onBindViewHolder(
        holder: RecyclerView.ViewHolder,
        position: Int
    ) {

        //Here you can fill your row view
        if (holder is VideoPlayerViewHolder) {
            val model = getItem(position)
            val genericViewHolder = holder

            // send data to view holder
            genericViewHolder.onBind(model)
        }
    }

    override fun onViewRecycled(holder: RecyclerView.ViewHolder) {
        val position = holder.adapterPosition
        PlayerViewAdapter.releaseRecycledPlayers(position)
        super.onViewRecycled(holder)
    }

    override fun getItemCount(): Int {
        return modelList.size
    }

    private fun getItem(position: Int): MediaObject {
        return modelList[position]
    }

    fun SetOnItemClickListener(mItemClickListener: OnItemClickListener?) {
        this.mItemClickListener = mItemClickListener
    }

    interface OnItemClickListener {
        fun onItemClick(
            view: View?,
            position: Int,
            model: MediaObject?
        )
    }

    inner class VideoPlayerViewHolder(private val binding: InstagramTimelineItemRecyclerBinding) : RecyclerView.ViewHolder(binding.root) {
        fun onBind(model: MediaObject) {
            // handel on item click
            binding.root.setOnClickListener {

                //TODO : Added this line
                model.isMute = !model.isMute
                notifyItemChanged(adapterPosition)

                mItemClickListener!!.onItemClick(
                    it,
                    adapterPosition,
                    model
                )
            }

            binding.apply {
                dataModel = model
                callback = this@InstagramRecyclerAdapter
                index = adapterPosition
                executePendingBindings()
            }
        }
    }

    override fun onVideoDurationRetrieved(duration: Long, player: Player) {
    }

    override fun onVideoBuffering(player: Player) {
    }

    override fun onStartedPlaying(player: Player) {

    }

    override fun onFinishedPlaying(player: Player) {
    }
}
Krutika-chotara commented 3 years ago

Is there any update on this. I am facing similar issue exo player become black after notify data set change.

Faiyyazauxy commented 3 years ago

i am using kohii library

mohitkapoor1230 commented 2 years ago

@Krutika-chotara @Faiyyazauxy @MostafaAnter any update on above issue ? I am also facing the same issue when I call "notifyItemCahnged(adapterPosition)".