我正在尝试创建一个简单的截屏应用程序,它可以捕获屏幕并将其保存到电影文件中。到目前为止,我的应用程序只生成了正确长度和大小的空电影文件(黑屏)。我不确定这是否有帮助,但是当我运行应用程序时,我会在 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;
}
感谢帮助。希望你们能够发现我错过的东西。