如何使用 MediaRecorder 记录现有表面(不是 PercientSurface)

Avi*_*ger 5 android surface mediarecorder kotlin android-mediacodec

我正在尝试记录我无权访问的摄像机流。我只能访问自定义 SurfaceView,它从相机预览中获取图像并渲染图像。

我想将表面\相机视频保存到文件中。我正在使用 SDK 进行流式传输 (Amazon Chime),但我无权访问相机组件来添加用于录制的持久表面。

虽然我确实可以访问每个新图像“VideoRenderer.I420Frame”,但不确定如何将其积累到视频中。

我想到的另一种方法是使用“MediaRecorder”中的表面,但是当我使用这个表面(此视图)时,我得到了这个异常:

java.lang.IllegalArgumentException:不是 PercientSurface

我可以以某种方式创建一个 PersistentSurface 并将当前表面复制到新表面中,同时获取新图像吗?或者以其他方式解决这个问题?

这个类目前看起来像这样:

class RecordingVideoRenderView : SurfaceViewRenderer, VideoRenderView {

private var recording: Boolean = false
private val logger = ConsoleLogger(LogLevel.DEBUG)
private val TAG = "RecordingVideoRenderView"
private lateinit var recorder: MediaRecorder

constructor(context: Context) : super(context)

constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

override fun renderFrame(frame: Any) {
    this.renderFrame(frame as VideoRenderer.I420Frame)
}

override fun initialize(initParams: Any?) {
    this.init((initParams as EglBase).eglBaseContext, null)
}

override fun finalize() {
    this.release()
}

fun startRecording() {
    logger.info(TAG, "startRecording")

    val recorderSurface = MediaCodec.createPersistentInputSurface()
    

    recorder = MediaRecorder()
    recorder.setVideoSource(MediaRecorder.VideoSource.SURFACE)

    // Getting java.lang.IllegalArgumentException: not a PersistentSurface when using:
    // this.holder.surface
    // I would like to copy the content of this.holder.surface to the new PersistentSurface
    recorder.setInputSurface(recorderSurface)

    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
    recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264)
    val outputDir = context.cacheDir
    val outputPath = File.createTempFile("test", "mp4", outputDir).absolutePath
    logger.info(TAG, "outputPath: $outputPath")
    recorder.setOutputFile(outputPath)
    recorder.prepare()
    recorder.start()
    recording = true
}

fun stopRecording() {
    logger.info(TAG, "stopRecording")
    recorder.stop()
    recording = false
}

fun isRecording(): Boolean {
    logger.info(TAG, "isRecording, recording:$recording")
    return recording
}
Run Code Online (Sandbox Code Playgroud)

}