【问题标题】:How to clear the GLSurfaceView如何清除 GLSurfaceView
【发布时间】:2014-07-21 07:51:03
【问题描述】:

我在视频流中使用 GLSurfaceView。一切顺利。但是当流结束时,GLSurfaceView 中还有剩余的图片。如何从 GLSurfaceView 中删除图片?

顺便说一句,当我跳到另一个活动并返回时,剩余的图片消失了。

--------------------更新-----------

我用这个解决了我的问题: GLSurfaceView.setVisiblility(View.Invisible); GLSurfaceView.setVisiblility(View.Visible); 所以 GLSurfaceView 可以重绘它的自身;

期待更好的答案。

【问题讨论】:

  • 我用这个解决了我的问题:

标签: android opengl-es-2.0 glsurfaceview


【解决方案1】:

视频流结束后尝试调用requestRender()。

【讨论】:

    【解决方案2】:

    试试这个神奇的代码:

        view.setRenderer(new Renderer() {
    
            @Override
            public void onSurfaceCreated(GL10 gl, EGLConfig config) {
            }
    
            @Override
            public void onSurfaceChanged(GL10 gl, int width, int height) {
            }
    
            @Override
            public void onDrawFrame(GL10 gl) {
                // here are Red, Green, Blue and Alpha values between 0 and 1
                GLES20.glClearColor(0, 0, 0, 1); 
                GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);                
            }
        });
    

    【讨论】:

      【解决方案3】:

      我在切换源(和视频组件)时遇到问题,但表面仍然显示旧帧,但我需要黑屏。

      什么对我有用,所有操作都在渲染器(impl.GLSurfaceView.Renderer)类中完成:

      1. 创建AtomicBoolean,初始值为false(示例中又名stopRendering);
      2. 清除表面的功能(通过 OpenGL):
       private void clearSurface() {
           GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
           GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
       }
      
      1. 然后函数清除和停止渲染(我在切换播放器VideoComponent时调用它):
       public void clearAndStopRendering() {
           stopRendering.set(true);
           glSurface.requestRender();
       }
      
      1. 当帧准备好时恢复帧渲染:
          @Override
         public void onVideoFrameAboutToBeRendered(long presentationTimeUs, long releaseTimeNs, @NonNull Format format, @Nullable MediaFormat mediaFormat) {
             if (stopRendering.compareAndSet(true, false)) {
                 glSurface.requestRender();
             }
         }
      

      我的onDrawFrame 的样子:

          @Override
          public void onDrawFrame(GL10 gl) {
              // some initialization code, related to my work      
      
              if (stopRendering.get()) {
                  clearSurface();
                  return;
              }
      
              // Drawing ...
          }
      

      我在GLSurfaceView 中的代码,我正在触发清除:

          public void setVideoComponent(@Nullable Player.VideoComponent newVideoComponent) {
              if (newVideoComponent == videoComponent) {
                  return;
              }
              if (videoComponent != null) {
                  // clearing old VideoComponent
                  renderer.clearAndStopRendering();
              }
              videoComponent = newVideoComponent;
              // setup new VideoComponent 
          }
      

      所以,我实现了什么:每次VideoComponent 更改(通过自定义PlayerView,其中包括我的GLSurfaceView)时,都会渲染黑屏,然后在视频帧准备好后渲染它,而不是旧框架。

      P.S. BTW 第二种变体,如果您像我一样使用自定义 PlayerView,您可以这样做:

      class MyGlPlayerView @JvmOverloads constructor(
          context: Context, attrs: AttributeSet? = null
      ) : PlayerView(context, attrs), MyPlayerView {
      
          private lateinit var glSurfaceView: MyGLSurfaceView
      
          override fun onFinishInflate() {
              super.onFinishInflate()
      
              val contentFrame = findViewById<FrameLayout>(R.id.exo_content_frame)
      
              glSurfaceView = MyGLSurfaceView(context, false);
      
              contentFrame.addView(glSurfaceView, 0)
              setShutterBackgroundColor(Color.BLACK)
          }
      
          override fun setPlayer(player: Player?) {
              glSurfaceView.setVideoComponent(player?.videoComponent)
      
              super.setPlayer(player)
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-13
        • 1970-01-01
        • 2020-03-11
        • 2011-12-24
        • 2011-01-16
        相关资源
        最近更新 更多