Stopping Exoplayer on swiping in viewPager

May 08, 2022, at 03:50 AM

I am making a reels type app.
I have made a ReelsFragment and ReelsAdapter.
I have used View Pager 2 to swipe views vertically.
I am using Exoplayer to play videos.

But my problem is:

When i swipe videos, the audio of 1st video still play or you can say overlap each other.

I have also used "onViewAttachedToWindow" and "onViewDetachToWindow" function but when i swipe video to 2nd video player from 1st video, it stop but player of 3rd video or you can say audio started overlapping with 2nd video audio also when i swipe back player not work only black screen shows . These problems occurs if i use above two function.

So what should i do to remove all these problems.
My code of Reels Adapter is:-

class ReelsAdapter(private val context: Context,
               private var reelsList: ArrayList<Videos>):
RecyclerView.Adapter<ReelsAdapter.VideoHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VideoHolder {
    return VideoHolder(ItemReelsBinding.inflate(LayoutInflater.from(context), parent, false))
override fun onBindViewHolder(holder: VideoHolder, position: Int) {
    holder.videoTitle.text = reelsList[position].title

override fun getItemCount(): Int {
    return reelsList.size

inner class VideoHolder(binding: ItemReelsBinding): RecyclerView.ViewHolder(binding.root) {
    var videoTitle = binding.videoTitle
    var progressBar = binding.progressBar
    var reelsPlayer = binding.reelsExoplayer
    lateinit var player: ExoPlayer

    fun setVideoData(videos: Videos){
        try {
        }catch (e: Exception){}
        player = ExoPlayer.Builder(context).build()
        reelsPlayer.player = player
        val mediaItem = MediaItem.fromUri(videos.videoUrl!!)
        player.apply {
        progressBar.visibility = View.GONE

override fun onViewAttachedToWindow(holder: VideoHolder) {
override fun onViewDetachedFromWindow(holder: VideoHolder) {

My code of ReelFragment:-

class ReelsFragment : Fragment() {
private lateinit var player: ExoPlayer
private lateinit var binding: FragmentReelsBinding
lateinit var dBRef: DatabaseReference
lateinit var reelList: ArrayList<Videos>
lateinit var reelsAdapter: ReelsAdapter

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View? {
    val view = layoutInflater.inflate(R.layout.fragment_reels, container, false)
    binding = FragmentReelsBinding.bind(view)
    reelList = ArrayList()
    reelList.add(Videos("abc", "this is 1 tile", ""))
    reelList.add(Videos("abc", "this is 2tile", ""))
    reelList.add(Videos("abc", "this is 3 tile", ""))
    reelList.add(Videos("abc", "this is 4 tile", "!%20%23Shorts.mp4?alt=media&token=713a0cb2-c122-4082-8d69-0f53a6dd30ba"))
    reelsAdapter = ReelsAdapter(requireContext(), reelList)
    binding.reelsViewPager.adapter = reelsAdapter

    return view
Rent Charter Buses Company
Weird White lines in Android Launcher?

Weird White lines in Android Launcher?

I'm currently programming and I noticed that there are strange white lines around the app in the Android LauncherWith other apps, everything is normal

Android, Microsoft Graph API - network unreachable

Android, Microsoft Graph API - network unreachable

I have followed the example at https://docsmicrosoft

Flutter parameters transfering across pages: What about the parameter &quot;key&quot;?

Flutter parameters transfering across pages: What about the parameter "key"?

I have made two pages, PersonalPage and InformationPageI want to transfer some parameters from PersonalPage to InformationPage

ffmpeg trim Unable to find a suitable output format

ffmpeg trim Unable to find a suitable output format

As stated in this post i try to split a video using mmpeg, slow down one of the parts and merge them back together