我使用
MediaRecorder
实现了Android的Camera2 API,从相机中录制视频。相机预览工作正常,视频文件也成功创建,没有任何错误。然而,输出的视频是空白的,有白色的闪烁线。
This is the output video I get.
准备
MediaRecorder
的代码
val surface = MediaCodec.createPersistentInputSurface()
surface.release()
val outputFile = createFile(applicationContext,"mp4")
val mMediaRecorder = MediaRecorder()
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC)
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE)
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
mMediaRecorder.setOutputFile(outputFile.absolutePath)
mMediaRecorder.setVideoEncodingBitRate(10_000_000)
mMediaRecorder.setVideoFrameRate(30)
mMediaRecorder.setVideoSize(matchedResolution.width, matchedResolution.height)
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264)
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC)
mMediaRecorder.setInputSurface(surface)
创建会议。
val previewSurface = surfaceView2.holder.surface
val captureCallbackVideo =
object : CameraCaptureSession.StateCallback() {
override fun onConfigureFailed(session: CameraCaptureSession) {}
override fun onConfigured(session: CameraCaptureSession) {
// session configured
val previewRequestBuilder =
cameraDevice.createCaptureRequest(TEMPLATE_RECORD)
.apply {
addTarget(previewSurface)
addTarget(surface)
set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, Range(30,30))
session.setRepeatingRequest(
previewRequestBuilder.build(),
null,
Handler { true }
cameraDevice.createCaptureSession(mutableListOf(previewSurface, surface), captureCallbackVideo, Handler { true })
用于开始录音
mMediaRecorder.prepare()
mMediaRecorder.start()
For stopping
mMediaRecorder.stop()
mMediaRecorder.release()