0
votes

I am using recyclerview to play videos using exoplayer from the database of realtime database. When I try to come back to the previous activity by pressing back button but the video's audio continues to play in the background.

Here is adapter code:-

class VideoAdapter(
    private var mContext: Context,
    private var mvideos: List<VID>
) : RecyclerView.Adapter<VideoAdapter.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(mContext).inflate(R.layout.videos_adapter, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val videoid = mvideos[position]

        holder.vidId.text = videoid.getilp()

        val dataSourceFactory: DataSource.Factory = DefaultHttpDataSource.Factory()
        val videoLink = Uri.parse(videoid.getVideoUrl())
        val mediaSource: MediaSource = ProgressiveMediaSource.Factory(dataSourceFactory)
            .createMediaSource(MediaItem.fromUri(videoLink))

        holder.mPlayer = SimpleExoPlayer.Builder(mContext).build()
        holder.playerView.player = holder.mPlayer
        holder.mPlayer!!.playWhenReady = true
        holder.mPlayer!!.setMediaSource(mediaSource)
        holder.mPlayer!!.stop()
    }

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

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var mPlayer: SimpleExoPlayer? = null
        var playerView: PlayerView = itemView.videos_ret
        var vidId: TextView = itemView.videos_id_ret
    }

}

Here is my activity code:-

class videos : AppCompatActivity() {

    val wimagesRef = FirebaseDatabase.getInstance().getReference().child("videos")

    private var recyclerView: RecyclerView? = null
    private var videoAdapter: VideoAdapter? = null
    private var vid1: MutableList<VID>? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_videos)

        recyclerView = videos
        recyclerView?.setHasFixedSize(true)
        recyclerView?.layoutManager = GridLayoutManager(this, 1)

        vid1 = ArrayList()
        videoAdapter = baseContext?.let { VideoAdapter(it, vid1 as ArrayList<VID>) }
        recyclerView?.adapter = videoAdapter

        wimagesRef.addValueEventListener(object : ValueEventListener {
            override fun onDataChange(dataSnapshot: DataSnapshot) {
                for (snapshot in dataSnapshot.children) {
                    videoAdapter?.notifyDataSetChanged()
                    val video = snapshot.getValue(VID::class.java)
                    if (video != null) {
                        videos.visibility = View.VISIBLE
                        (vid1 as ArrayList<VID>).add(video)
                    }
                }
            }

            override fun onCancelled(error: DatabaseError) {

            }
        })
    }

    override fun onBackPressed() {
        super.onBackPressed()

        clearAll()
    }

    fun clearAll() {


        vid1?.clear()
        videoAdapter = baseContext?.let { VideoAdapter(it, vid1) }
        videoAdapter?.notifyDataSetChanged()
        finish()
    }

    override fun onDestroy() {
        super.onDestroy()

        clearAll()
    }
}

I want to send empty list in the adapter to remove all the data in the adapter present. Can someone please help me?

Check ListAdapter and how it can be used with recyclerviewrahat
Sorry but I didn't get it.CHETAN
I just want to remove all the data in recyclerview after pressing back button to stop exoplayer.CHETAN