拿这个示例代码块:
// from window manager
val recordWidth = screenWidth
val recordHeight = screenHeight
val projection: MediaProjection = // retrieved from API
val mediaRecorder = MediaRecorder().apply {
setVideoSource(SURFACE)
setOutputFormat(MPEG_4)
setVideoFrameRate(frameRate) // e.g. 30
setVideoEncoder(H264)
setVideoSize(recordWidth, recordHeight)
setVideoEncodingBitRate(videoBitRate)
setOutputFile(outputFile)
prepare()
}
val virtualDisplay: VirtualDisplay = projection?.createVirtualDisplay(
"Example",
screenWidth,
screenHeight,
screenDensity,
VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
mediaRecorder.surface,
null,
null
)
mediaRecorder.start()
这一切都很好,只要screenWidth和screenHeight与显示的匹配。
如果我更改recordWidth和recordHeight(使用MediaRecorder
传递给setVideoSize(Int, Int)
),那么一切都会出错。录制的视频往往只包含整个屏幕的左上部分。
所以我的主要问题是:
Camera
提供了一个,但这并不是真的使用Camera API。我想你可以参考这个.mMediaProjection = mMediaProjectionManager.getMediaProjection(resultCode,resultData);
Log.d(TAG, "startRecording...");
this.mVideoBufferInfo = 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);