Stopping Exoplayer on swiping in viewPager

175
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
    holder.setVideoData(reelsList[position])
}

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 {
            player.release()
        }catch (e: Exception){}
        player = ExoPlayer.Builder(context).build()
        reelsPlayer.player = player
        val mediaItem = MediaItem.fromUri(videos.videoUrl!!)
        player.apply {
            this.setMediaItem(mediaItem)
            this.prepare()
        }
        progressBar.visibility = View.GONE
        player.play()

    }
}
override fun onViewAttachedToWindow(holder: VideoHolder) {
    super.onViewAttachedToWindow(holder)
    holder.player.play()
}
override fun onViewDetachedFromWindow(holder: VideoHolder) {
    super.onViewDetachedFromWindow(holder)
    holder.player.release()
    holder.player.prepare()
    holder.player.play()
}

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", "https://firebasestorage.googleapis.com/v0/b/klarify-3f5bc.appspot.com/o/Labour%20Law%20Advisor%2FA4%20(EPS%20increases%20BUT%20Profit%20DOES%20NOT%20%23Shorts.mp4?alt=media&token=2cac5508-21ba-48c1-8327-9a58f1d5a2c9"))
    reelList.add(Videos("abc", "this is 2tile", "https://firebasestorage.googleapis.com/v0/b/klarify-3f5bc.appspot.com/o/Labour%20Law%20Advisor%2FA15%20(Buy%20land%20with%2010%2C000--%20%23Shorts.mp4?alt=media&token=9f125381-7d55-494d-ab40-aa814309d415"))
    reelList.add(Videos("abc", "this is 3 tile", "https://firebasestorage.googleapis.com/v0/b/klarify-3f5bc.appspot.com/o/Labour%20Law%20Advisor%2FA8%20(7%25%20of%20PPF%20Better%20than%2011%25%20of%20Mutual%20Funds.%20HOW%20%23shorts.mp4?alt=media&token=2aee97ea-de77-4b8c-8e3c-103d6b34cdf3"))
    reelList.add(Videos("abc", "this is 4 tile", "https://firebasestorage.googleapis.com/v0/b/klarify-3f5bc.appspot.com/o/Labour%20Law%20Advisor%2FA5%20(Printing%20Money%20is%20not%20the%20solution!%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
READ ALSO
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

112
Android, Microsoft Graph API - network unreachable

Android, Microsoft Graph API - network unreachable

I have followed the example at https://docsmicrosoft

189
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

118
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

112