使用PagerAdapter和ScalableVideoView(如VideoView)更改页面时,如何在上一页释放视频/媒体

时间:2018-08-31 17:29:21

标签: android kotlin android-viewpager android-pageradapter

这是我在PagerAdapter上的代码(kotlin代码)

class SwipeAdapter : PagerAdapter {
    var videoResources :ArrayList<Int>
    lateinit var videoBackground : ScalableVideoView
    private var ctx: Context

    constructor(ctx: Context, videoResources: ArrayList<Int>) : super(){
        this.videoResources = videoResources
        this.ctx = ctx
    }
    override fun isViewFromObject(view: View?, `object`: Any?): Boolean {
        return (view == `object` as FrameLayout)
    }

    override fun getCount(): Int {
        return videoResources.count()
    }

    override fun instantiateItem(container: ViewGroup?, position: Int):View{
        val layoutInflater = ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        val rootView =  layoutInflater.inflate(R.layout.view_breath_fragment_slide,container,false)
        this.videoBackground = rootView.findViewById(R.id.videoBackground)


    .....


            try {
                videoBackground.setRawData(videoResources.get(position))
                videoBackground.setVolume(0f, 0f)
                videoBackground.isLooping = true
                videoBackground.prepare { videoBackground.start()}
            } catch (ioe: IOException) {
                //ignore
            }
            videoBackground.visibility = View.VISIBLE
....
        container?.addView(rootView)
        return rootView
    }

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

}

这是我在切换到另一个应用程序或只是首页

时继续播放视频/媒体而得到的错误

A / OpenGLRenderer:泄漏了2个GPU对象! A / libc:致命信号6(SIGABRT),tid 19002(RenderThread)中的代码-6


我尝试发布像这样的视频/媒体:

 if(videoBackground!=null)
       videoBackground.release()

或 我试过destroyItem:

 override fun instantiateItem(container: ViewGroup?, position: Int):View{
    val layoutInflater = ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    val rootView =  layoutInflater.inflate(R.layout.view_breath_fragment_slide,container,false)
    rootView.findViewById(R.id.videoBackground).release()

但这无济于事,因为它没有访问上一页对象

另一次失败尝试,但在MainActivity上基本相同:(java)

viewPager.getAdapter().videoBackground.release()

更多次失败的尝试,但我认为它已经足够表明我在同一圈子中行走

0 个答案:

没有答案