在我发布使用它的Mediacodec后,我可以在surfaceView上绘图

时间:2017-01-31 23:46:54

标签: android canvas surfaceview h.264 mediacodec

我目前正在使用mediacodec进行实时视图硬件解码。 代码看起来像这样

mMediaCodecDecoder = MediaCodec.createDecoderByType(MIME_TYPE);
format = MediaFormat.createVideoFormat(MIME_TYPE, mLiveViewBuff.frameData.picWidth, mLiveViewBuff.frameData.picHeight);
if (mSurfaceHolder.getSurface().isValid()) {
    mMediaCodecDecoder.configure(format, mSurfaceHolder.getSurface(), null, 0);
}
mMediaCodecDecoder.start();
decoderInputBuffers = mMediaCodecDecoder.getInputBuffers();
decoderOutputBuffers = mMediaCodecDecoder.getOutputBuffers();
decoderConfigured = true;

在某个阶段,我决定像这样发布Mediacodec

mMediaCodecDecoder.stop();
mMediaCodecDecoder.release();
mMediaCodecDecoder = null;

之后我想在我想为另一个liveview硬件解码创建另一个mediacodec之前通过这样做来清除表面视图。

Canvas canvas = mSurfaceHolder.lockCanvas();
canvas.drawColor(getResources().getColor(R.color.WHITE));
mSurfaceHolder.unlockCanvasAndPost(canvas);

但我收到了一些connect(P): already connected错误

长话短说

所以基本上,我有2 h264 livefeed使用mediacodec进行硬件解码并在surfaceview上播放。在玩它们之间,我想通过在画布上画出纯黑色来清除表面视图。

请给我一些建议 感谢

1 个答案:

答案 0 :(得分:0)

我在这里得到类似的问题并在这里回答https://stackoverflow.com/a/29243067/849715

private void clearSurface(SurfaceTexture texture) {
        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);
    }