用于屏幕捕获的MediaProjection - 如何更改分辨率?

问题描述 投票:0回答:1

拿这个示例代码块:

// 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)),那么一切都会出错。录制的视频往往只包含整个屏幕的左上部分。

所以我的主要问题是:

  1. 在录制屏幕时是否需要做一些特殊的事情来降低分辨率?即使我保持宽高比与屏幕,它似乎不起作用。
  2. 某些宽度/高度值会导致崩溃 - 是否有API来检索支持的屏幕录制大小?我知道Camera提供了一个,但这并不是真的使用Camera API。
android mediarecorder screen-capture mediaprojection
1个回答
1
投票

我想你可以参考这个.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);
© www.soinside.com 2019 - 2024. All rights reserved.