Release resources in PagerAdapter

85 Views Asked by At

I have a PagerAdapter, and in instantiateItem i initialize an ExoPlayer.

My question is, how do I release the player?

I assume, somehow the function destroyItem should be involved, but destroyItem only gehts the view as an object. How can I release resources specific to this one item of the PagerAdapter?

Here my source, if someone is interested:

override fun instantiateItem(container: ViewGroup, position: Int): Any {
    [...]
        url.contains(mContext.getString(R.string.video_file_indicator)) -> {

            val exoPlayer = ExoPlayerFactory.newSimpleInstance(mContext)
            videoView.player = exoPlayer

            val dataSourceFactory : DataSource.Factory = DefaultDataSourceFactory(mContext, Util.getUserAgent(mContext, mContext.getString(R.string.app_name)))
            var videoSource : MediaSource = ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(url))
            if (url == Helper.getUserProfile().profileVideoUrl) {
                val localFile = File(mContext.getExternalFilesDir(null), Helper.profilePicVideoName)
                videoSource = ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(localFile.toUri())                   
            }
            exoPlayer.prepare(videoSource)
            exoPlayer.playWhenReady = true
            exoPlayer.repeatMode = Player.REPEAT_MODE_ONE 
            videoView.resizeMode = AspectRatioFrameLayout.RESIZE_MODE_ZOOM
        }          
    }
    container.addView(layout)
    return layout
}

override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
    container.removeView(`object` as View)
}
1

There are 1 best solutions below

1
Giorgio Antonioli On BEST ANSWER

You don't need to return a View from the method instantiateItem(), you can also return a wrapper containing your ExoPlayer and your View.

e.g.

data class Wrapper(val view: View, val player: ExoPlayer)

And in your PagerAdapter:

override fun instantiateItem(container: ViewGroup, position: Int): Any {
    [...]
    return Wrapper(layout, exoPlayer)
}

override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
    val wrapper = `object` as Wrapper
    container.removeView(wrapper.view)
    // Release the player here.
    wrapper.player.doSomething()
}

If you want instead to return a view from instantiateItem() you can assign the ExoPlayer as the tag of the view to retrieve it later.

e.g.

override fun instantiateItem(container: ViewGroup, position: Int): Any {
    [...]
    return layout.apply {
        setTag(exoPlayer)
    }
}

override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
    val view = `object` as View
    container.removeView(view)
    // Release the player here.
    val exoPlayer = view.getTag() as ExoPlayer
    exoPlayer.doSomething()
}