Prashant_Sarin
Prashant_Sarin

Reputation: 114

Exoplayer in recyclerView gives out of memory exception

RecyclerView with each item as an Exoplayer throws out of memory exception even after releasing the player in onViewRecycled() method of adapter. what i can do to resolve this issue?

i have gone through other posts about this issue, out of that i tried this link but this also is not working.

And i have already increased the heapSize in manifest.

snippet of code from adapter:

     override fun onBindViewHolder(holder: MemoryHomeHolder, position:Int) 
     {
        val memory = memoryList[position]
        holder.bind(memory)
        holder.itemMemoryListBinding.playerView?.useController = true
        holder.itemMemoryListBinding.playerView?.showController()

        val path: memory.path

        val player = ExoPlayerFactory.newSimpleInstance(
            context,
            DefaultTrackSelector(),
            DefaultLoadControl()
        )
        val dataSourceFactory = DefaultDataSourceFactory(
            context,
            Util.getUserAgent(context, AppConstants.OFH)
        )
        val source = ProgressiveMediaSource.Factory(dataSourceFactory) .createMediaSource(Uri.parse(path))
        holder.itemMemoryListBinding.playerView?.player = player

        holder.itemMemoryListBinding.playerView?.controllerHideOnTouch = false
        holder.itemMemoryListBinding.playerView?.controllerShowTimeoutMs = -1
        holder.itemMemoryListBinding.playerView?.resizeMode =
        AspectRatioFrameLayout.RESIZE_MODE_FILL
        holder.itemMemoryListBinding.playerView?.exo_progress?.visibility 
        = View.GONE
        (holder.itemMemoryListBinding.playerView?.player as 
         SimpleExoPlayer?)?.prepare(source)
        (holder.itemMemoryListBinding.playerView?.player as 
        SimpleExoPlayer?)?.repeatMode = Player.REPEAT_MODE_ALL

        activePlayerHolders.add(holder)
        Log.d(TAG, "holder init for position = $position")
   }

   override fun onViewRecycled(holder: MemoryHomeHolder) {
        super.onViewRecycled(holder)
        holder.itemMemoryListBinding.playerView?.player?.release()
   }

I want the RecyclerView to have Exoplayer as an item in every holder and it should not throw out of memory exception

Upvotes: 2

Views: 1323

Answers (1)

Prashant_Sarin
Prashant_Sarin

Reputation: 114

I was not releasing the player properly on onViewRecycled() method, which created out of memory exception.

you can simply release the player of the holder being released as below:

holder?.playerView?.player?.release()

This fixed the issue.

Upvotes: 1

Related Questions