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)
}
You don't need to return a
Viewfrom the methodinstantiateItem(), you can also return a wrapper containing yourExoPlayerand yourView.e.g.
And in your
PagerAdapter:If you want instead to return a view from
instantiateItem()you can assign theExoPlayeras the tag of the view to retrieve it later.e.g.