释放 PagerAdapter 中的资源
Release resources in PagerAdapter
我有一个 PagerAdapter,在 instantiateItem
中我初始化了一个 ExoPlayer
。
我的问题是,如何释放播放器?
我想,应该以某种方式涉及函数 destroyItem
,但 destroyItem
仅将视图作为对象。如何释放特定于 PagerAdapter
中的这一项的资源?
这是我的来源,如果有人感兴趣:
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)
}
您不需要 return 来自方法 instantiateItem()
的 View
,您还可以 return 包含您的 ExoPlayer
和您的View
.
例如
data class Wrapper(val view: View, val player: ExoPlayer)
在你的 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()
}
如果您想要 return 来自 instantiateItem()
的视图,您可以将 ExoPlayer
指定为视图的标签以便稍后检索它。
例如
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()
}
我有一个 PagerAdapter,在 instantiateItem
中我初始化了一个 ExoPlayer
。
我的问题是,如何释放播放器?
我想,应该以某种方式涉及函数 destroyItem
,但 destroyItem
仅将视图作为对象。如何释放特定于 PagerAdapter
中的这一项的资源?
这是我的来源,如果有人感兴趣:
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)
}
您不需要 return 来自方法 instantiateItem()
的 View
,您还可以 return 包含您的 ExoPlayer
和您的View
.
例如
data class Wrapper(val view: View, val player: ExoPlayer)
在你的 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()
}
如果您想要 return 来自 instantiateItem()
的视图,您可以将 ExoPlayer
指定为视图的标签以便稍后检索它。
例如
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()
}