android.media.MediaCodec $ CodecException:错误0x80001000

时间:2019-01-21 18:44:51

标签: java android

这三天以来,这个错误一直伴随着我,我在github上尝试了许多其他代码,而Google进行了相同的错误搜索,没有任何帮助,这就是我的功能

 private void startScreenCapture(int resultCode, Intent resultData, String format, int width, int height, int dpi, int bitrate) {
    this.mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, resultData);

    Log.d(TAG, "startRecording...");


    Log.e(TAG, "VideoFormat:" + format);
    Log.e(TAG, "Bitrate:" + bitrate);
    Log.e(TAG, "ScreenWidth:" + width);
    Log.e(TAG, "ScreenHeight:" + height);
    Log.e(TAG, "ScreenDpi:" + dpi);


    this.videoBufferInfo = new MediaCodec.BufferInfo();
    MediaFormat mediaFormat = MediaFormat.createVideoFormat(format, width, height);

    mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, bitrate);
    mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, FPS);
    mediaFormat.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 0);
    mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1);

    mediaFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 0);




    try {

        switch (format) {
            case MediaFormat.MIMETYPE_VIDEO_AVC:
                // AVC
                mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);

                this.encoder = MediaCodec.createEncoderByType(format);
                this.encoder.setCallback(new MediaCodec.Callback() {
                    @Override
                    public void onInputBufferAvailable(MediaCodec codec, int inputBufferId) {
                    }

                    @Override
                    public void onOutputBufferAvailable(MediaCodec codec, int outputBufferId, MediaCodec.BufferInfo info) {
                        ByteBuffer outputBuffer = codec.getOutputBuffer(outputBufferId);
                        if (info.size > 0 && outputBuffer != null) {
                            outputBuffer.position(info.offset);
                            outputBuffer.limit(info.offset + info.size);
                            byte[] b = new byte[outputBuffer.remaining()];
                            outputBuffer.get(b);
                            sendData(null, b);
                        }
                        if (encoder != null) {
                            encoder.releaseOutputBuffer(outputBufferId, false);
                        }
                        if ((videoBufferInfo.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
                            Log.i(TAG, "End of Stream");
                            stopScreenCapture();
                        }
                    }

                    @Override
                    public void onError(MediaCodec codec, MediaCodec.CodecException e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onOutputFormatChanged(MediaCodec codec, MediaFormat format) {
                        Log.i(TAG, "onOutputFormatChanged. CodecInfo:" + codec.getCodecInfo().toString() + " MediaFormat:" + format.toString());
                    }
                });
                break;
            case MediaFormat.MIMETYPE_VIDEO_VP8:
                mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Flexible);
                final int frameSize = width * height * 3 / 2;
                //VP8
                byte[] ivfHeader = IvfWriter.makeIvfHeader(0, width, height, 1, bitrate);
                sendData(null, ivfHeader);

                this.encoder = 
     //      MediaCodec.createByCodecName("OMX.google.vp8.encoder");
         this.encoder = MediaCodec.createEncoderByType(format);
                this.encoder.setCallback(new MediaCodec.Callback() {
                    @Override
                    public void onInputBufferAvailable(MediaCodec codec, int inputBufIndex) {
                    }

                    @Override
                    public void onOutputBufferAvailable(MediaCodec codec, int outputBufferId, MediaCodec.BufferInfo info) {
                        ByteBuffer outputBuffer = codec.getOutputBuffer(outputBufferId);
                        if (info.size > 0 && outputBuffer != null) {
                            outputBuffer.position(info.offset);
                            outputBuffer.limit(info.offset + info.size);

                            byte[] header = IvfWriter.makeIvfFrameHeader(outputBuffer.remaining(), info.presentationTimeUs);
                            byte[] b = new byte[outputBuffer.remaining()];
                            outputBuffer.get(b);

                            sendData(header, b);
                        }
                        if (encoder != null) {
                            encoder.releaseOutputBuffer(outputBufferId, false);
                        }
                        if ((videoBufferInfo.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
                            Log.i(TAG, "End of Stream");
                            stopScreenCapture();
                        }
                    }

                    @Override
                    public void onError(MediaCodec codec, MediaCodec.CodecException e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onOutputFormatChanged(MediaCodec codec, MediaFormat format) {
                        Log.i(TAG, "onOutputFormatChanged. CodecInfo:" + codec.getCodecInfo().toString() + " MediaFormat:" + format.toString());
                    }
                });
                break;
            default:
                throw new RuntimeException("Unknown Media Format. You need to add mimetype to string.xml and else if statement");
        }

        this.encoder.configure(mediaFormat
                , null // surface
                , null // crypto
                , MediaCodec.CONFIGURE_FLAG_ENCODE);

        this.inputSurface = this.encoder.createInputSurface();
        this.encoder.start();

    } catch (IOException e) {
        Log.e(TAG, "Failed to initial encoder, e: " + e);
        releaseEncoders();
    }

    this.virtualDisplay = this.mediaProjection.createVirtualDisplay("Recording Display", width, height, dpi, 0, this.inputSurface, null, null);
}

我使用的宽度,高度和dpi是

VideoFormat:video/avc
Bitrate:6144000
ScreenWidth:640
ScreenHeight:360
ScreenDpi:96

更改宽度和高度相同的东西相同的错误 彼此替换宽度和高度值相同的错误 在谷歌搜索没有什么可以编辑只是建议更改高度,我做到了 和其他选项的添加

 mediaFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 0);

我将其添加为相同的错误,在模拟器上尝试相同的错误 在其他移动LG G5中尝试相同的错误 最后问到这里,我希望找到解决方法

来自的代码 here

0 个答案:

没有答案