Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Player stop working when we call notifydatasetchanged() #7

Open
zakirhussain49 opened this issue Apr 8, 2021 · 5 comments
Open

Player stop working when we call notifydatasetchanged() #7

zakirhussain49 opened this issue Apr 8, 2021 · 5 comments

Comments

@zakirhussain49
Copy link

zakirhussain49 commented Apr 8, 2021

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
Copy link
Owner

Share your code so i can check it

@Faiyyazauxy
Copy link

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
Copy link

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

@Faiyyazauxy
Copy link

i am using kohii library

@mohitkapoor1230
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants