美文网首页
TextureView 使用报错异常detachFromGLCo

TextureView 使用报错异常detachFromGLCo

作者: GexYY | 来源:发表于2018-04-26 11:02 被阅读0次

    Log日志异常信息

    java.lang.RuntimeException
    Error during detachFromGLContext (see logcat for details)
    
    android.graphics.SurfaceTexture.detachFromGLContext(SurfaceTexture.java:179)
    android.view.TextureView.destroySurface(TextureView.java:226)
    android.view.TextureView.access$000(TextureView.java:105)
    android.view.TextureView$1.run(TextureView.java:214)
    android.view.HardwareRenderer$Gl20Renderer.safelyRun(HardwareRenderer.java:1528)
    android.view.TextureView.onDetachedFromWindow(TextureView.java:211)
    android.view.View.dispatchDetachedFromWindow(View.java:11810)
    android.view.ViewGroup.removeAllViewsInLayout(ViewGroup.java:3)
    android.view.ViewGroup.removeAllViews(ViewGroup.java:3765)
    

    Android系统4.4之前的版本(eg:4.0或4.1)系统,可能会异常报错,主要原因是因为TextureView必须持有一个活跃未被释放的SurfaceTexture,在onSurfaceTextureDestroyed(SurfaceTexture surface)方法回掉用,要准备的返回true和false,而不能一直返回true;

    原因我们看一下TextureView内部源码会发现,如果返回true的时机不合适,SurfaceTexture就会被销毁和释放,就会有概率出现异常

    源码.png

    解决办法1:
    参考Bilibili的ijkplayer内部实现
    https://github.com/Bilibili/ijkplayer/blob/master/android/ijkplayer/ijkplayer-example/src/main/java/tv/danmaku/ijk/media/example/widget/media/TextureRenderView.java

    解决办法2:
    在onSurfaceTextureDestroyed()方法中一直返回false,如果需要一个比较长生命周期的SurfaceTexture,但是一定要在onDetachedFromWindow()方法中做释放操作.

       @Override
        protected void onDetachedFromWindow(){
            super.onDetachedFromWindow();
            if (mSurfaceTexture != null)  {
                mSurfaceTexture.release();
                mSurfaceTexture = null;
            }
            if (mSurface != null) {
                mSurface.release();
                mSurface = null;
            }
        }
    

    相关文章

      网友评论

          本文标题:TextureView 使用报错异常detachFromGLCo

          本文链接:https://www.haomeiwen.com/subject/nwdplftx.html