用黑色矩形清洁SurfaceTexture

时间:2015-05-11 10:23:39

标签: android exoplayer android-textureview

我正在使用 Exoplayer GL SurfaceTexture (来自TextureView)来显示视频。我在视频播放中重用相同的表面

我释放播放器并实例化一个新播放器。第二次显示SurfaceTexture时,会显示上一个视频的旧纹理,直到播放器开始播放并用黑色填充Surface。

我正在寻找一种方法用黑色绘制黑色矩形以填充表面,但无法实现此目的。

3 个答案:

答案 0 :(得分:12)

在Grafika上使用@fadden链接,我已经制作了自己的脚本来清除表面。它与API 16兼容。

GIST

/**
 * Clear the given surface Texture by attaching a GL context and clearing the surface.
 * @param texture a valid SurfaceTexture
 */
private void clearSurface(SurfaceTexture texture) {
    if(texture == null){
        return;
    }

    EGL10 egl = (EGL10) EGLContext.getEGL();
    EGLDisplay display = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
    egl.eglInitialize(display, null);

    int[] attribList = {
            EGL10.EGL_RED_SIZE, 8,
            EGL10.EGL_GREEN_SIZE, 8,
            EGL10.EGL_BLUE_SIZE, 8,
            EGL10.EGL_ALPHA_SIZE, 8,
            EGL10.EGL_RENDERABLE_TYPE, EGL10.EGL_WINDOW_BIT,
            EGL10.EGL_NONE, 0,      // placeholder for recordable [@-3]
            EGL10.EGL_NONE
    };
    EGLConfig[] configs = new EGLConfig[1];
    int[] numConfigs = new int[1];
    egl.eglChooseConfig(display, attribList, configs, configs.length, numConfigs);
    EGLConfig config = configs[0];
    EGLContext context = egl.eglCreateContext(display, config, EGL10.EGL_NO_CONTEXT, new int[]{
            12440, 2,
            EGL10.EGL_NONE
    });
    EGLSurface eglSurface = egl.eglCreateWindowSurface(display, config, texture,
            new int[]{
                    EGL10.EGL_NONE
            });

    egl.eglMakeCurrent(display, eglSurface, eglSurface, context);
    GLES20.glClearColor(0, 0, 0, 1);
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
    egl.eglSwapBuffers(display, eglSurface);
    egl.eglDestroySurface(display, eglSurface);
    egl.eglMakeCurrent(display, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE,
            EGL10.EGL_NO_CONTEXT);
    egl.eglDestroyContext(display, context);
    egl.eglTerminate(display);
}

答案 1 :(得分:2)

我不熟悉Exoplayer,但我怀疑你的选项与使用this answer中列出的MediaPlayer播放的选项相同。

要点:

  • 您可以附加GLES,清除Surface,分离GLES(Grafika does this)。
  • 您可以创建一个单帧黑色视频并进行播放。
  • 您可以创建第二个空视图,它覆盖第一个视图。在视频播放时隐藏它,并在视频播放时显示它。

你不能做的是附加一个画布并清除它,因为一旦连接了画布,它永远不会放手。

答案 2 :(得分:0)

请致电 GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

在绘制新框架之前