为了解决这个问题,可以使用以下代码示例:
MediaFormat outputFormat = mEncoder.getOutputFormat();
outputFormat.setInteger( MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
mEncoder.configure(outputFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
使用 OpenGL 渲染器编辑图像,以消除崩溃/重复/模糊等缺陷,代码示例如下:
public class Renderer implements GLSurfaceView.Renderer {
private int mTextureId;
private SurfaceTexture mSurfaceTexture;
private boolean mUpdateSurface = false;
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// 省略初始化代码
mTextureId = GLES20.GL_NONE;
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
// 省略 Surface 改变代码
}
@Override
public void onDrawFrame(GL10 gl) {
// 省略绘图代码
}
public SurfaceTexture getSurfaceTexture() {
return mSurfaceTexture;
}
public void updateTexture() {
mUpdateSurface = true;
}
private void createTexture() {
if (mTextureId != GLES20.GL_NONE) {
GLES20.glDeleteTextures(1, new int[]{mTextureId}, 0);
}
mTextureId = TextureHelper.createTexture();
mSurfaceTexture = new SurfaceTexture(mTextureId);
mSurfaceTexture.setOnFrameAvailableListener(surfaceTexture -> updateTexture());
}
}
在使用此输出表面之前,需要等待从 SurfaceTexture 中获得第一帧数据。并且要在主线程上更新 SurfaceTexture,代码示例如下:
Surface surface = MediaCodec.createPersistentInputSurface(); mEncoder.setInputSurface(surface);
SurfaceTexture surfaceTexture = mRenderer.getSurfaceTexture(); surfaceTexture.setDefaultBufferSize(width, height);
while (!mRenderer.is