Android媒体投影会生成一个空的电影文件

时间:2015-08-29 15:04:05

标签: android android-5.0-lollipop mediacodec android-mediaprojection

我正在尝试创建一个简单的截屏应用,它可以捕获屏幕并保存到电影文件中。到目前为止,我的应用程序一直只生成正确长度和大小的空白电影文件(黑屏)。我不确定这是否有帮助,但是当我运行应用程序时,我在logcat中收到这些消息

08-29 14:47:57.722    1973-1991/com.example.stw.myapplication W/EGL_emulation﹕ eglSurfaceAttrib not implemented
08-29 14:47:57.722    1973-1991/com.example.stw.myapplication W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xabdbee60, error=EGL_SUCCESS
08-29 14:47:58.870    1973-1991/com.example.stw.myapplication E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xab659760
08-29 14:47:58.875    1973-1991/com.example.kamil.myapplication D/OpenGLRenderer﹕ endAllStagingAnimators on 0xab32c580 (ListPopupWindow$DropDownListView) with handle 0xa333d8d0
08-29 14:48:00.652    1973-1973/com.example.kamil.myapplication D/MPEG4Writer﹕ Video track stopping
08-29 14:48:00.652    1973-1973/com.example.kamil.myapplication D/MPEG4Writer﹕ Video track source stopping
08-29 14:48:00.652    1973-1973/com.example.kamil.myapplication D/MPEG4Writer﹕ Video track source stopped

除此之外,这些消息似乎无关紧要。 我会发布一些代码,也许会有所帮助。 我得到了表面

mInputSurface = mVideoEncoder.createInputSurface();

所以我正在创建虚拟显示器:

        mMediaProjection.createVirtualDisplay("Recording Display", screenWidth,
            screenHeight, screenDensity, 0 /* flags */, mInputSurface,
            null /* callback */, null /* handler */);

然后调用此方法:

    private void prepareVideoEncoder() {
    mVideoBufferInfo = new MediaCodec.BufferInfo();
    MediaFormat format = MediaFormat.createVideoFormat(VIDEO_MIME_TYPE, VIDEO_WIDTH, VIDEO_HEIGHT);
    int frameRate = 30; // 30 fps

    // Set some required properties. The media codec may fail if these aren't defined.
    format.setInteger(MediaFormat.KEY_COLOR_FORMAT,
            MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
    format.setInteger(MediaFormat.KEY_BIT_RATE, 6000000); // 6Mbps
    format.setInteger(MediaFormat.KEY_FRAME_RATE, frameRate);
    format.setInteger(MediaFormat.KEY_CAPTURE_RATE, frameRate);
    format.setInteger(MediaFormat.KEY_REPEAT_PREVIOUS_FRAME_AFTER, 1000000 / frameRate);
    format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
    format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1); // 1 seconds between I-frames

    // Create a MediaCodec encoder and configure it. Get a Surface we can use for recording into.
    try {
        mVideoEncoder = MediaCodec.createEncoderByType(VIDEO_MIME_TYPE);
        mVideoEncoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
        mInputSurface = mVideoEncoder.createInputSurface();
        mVideoEncoder.start();
    } catch (IOException e) {
        releaseEncoders();
    }
}

然后,当用户点击停止录制时,我会发布如下编码器:

    private void releaseEncoders() {
    mDrainHandler.removeCallbacks(mDrainEncoderRunnable);
    if (mMuxer != null) {
        if (mMuxerStarted) {
            mMuxer.stop();
        }
        mMuxer.release();
        mMuxer = null;
        mMuxerStarted = false;
    }
    if (mVideoEncoder != null) {
        mVideoEncoder.stop();
        mVideoEncoder.release();
        mVideoEncoder = null;
    }
    if (mInputSurface != null) {
        mInputSurface.release();
        mInputSurface = null;
    }
    if (mMediaProjection != null) {
        mMediaProjection.stop();
        mMediaProjection = null;
    }
    mVideoBufferInfo = null;
    mDrainEncoderRunnable = null;
    mTrackIndex = -1;
}

感谢您的帮助。希望你们能够发现我错过的东西。

0 个答案:

没有答案