我正在制作一个Android Things应用,该应用使用 rasberry pie 3 摄像头模块记录x倍的视频,当我尝试打开新创建的 mp4 文件时,说它无法播放?
我还注意到,尽管强迫我的应用记录更长的时间(10分钟),但所有视频片段的文件大小均为3.4kb,也就是3.4kb
也许我没有正确设置它?我在日志中看不到任何错误。
以下是我的代码,该代码创建了供相机使用的表面对象:
private fun setMediaOutputSurface(): Surface {
var file = File(context.getExternalFilesDir(null).absolutePath)
if (file.canWrite() && file.canRead()) {
Log.d("JJJ", "File location is fine")
var files = file.listFiles()
for (item in files) {
Log.d("JJJ", "File is " + item.name)
}
}
val texture = mTextureView.surfaceTexture
texture.setDefaultBufferSize(mTextureView.width, mTextureView.height)
val surfaces = ArrayList<Surface>()
val surfacePreview = Surface(texture)
mediaRecorder = MediaRecorder().apply {
setVideoSource(MediaRecorder.VideoSource.SURFACE)
setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
setOutputFile(context.filesDir.absolutePath + "/" + System.currentTimeMillis() + ".mp4")
setVideoEncoder(MediaRecorder.VideoEncoder.H264)
setVideoSize(1920, 1080)
setVideoFrameRate(30)
setVideoEncodingBitRate(512 * 1000)
setPreviewDisplay(surfacePreview)
prepare()
Log.d("JJJ", "Finished preparing on file path " + context.filesDir.absolutePath + "/" + System.currentTimeMillis() + ".mp4")
}
return mediaRecorder.surface
}
这是相机状态回调
var cameraStateCalback = object : CameraDevice.StateCallback() {
override fun onOpened(camera: CameraDevice?) {
if (camera != null) {
Log.d("JJJ", "onOpened and will now create handler and capture session")
//create handler thread
val thread = HandlerThread("MyHandlerThread")
thread.start()
val handler = Handler(thread.looper)
//create capture session
val mediaRecorderSurface = setMediaOutputSurface()
Log.d("JJJ", "About to create capture session")
camera.createCaptureRequest(TEMPLATE_RECORD).build()
camera.createCaptureSession(Collections.singletonList(mediaRecorderSurface), cameraCaptureSessionCallBack, handler)
Log.d("JJJ", "Created thread handler and capture session")
}
}
这是开始记录的功能
override fun startRecording(videoCameraCallback: VideoCameraCallback, textureView: TextureView) {
mTextureView = textureView
val cameraIdList = cameraManager.cameraIdList
val cameraCharacteristics = cameraManager.getCameraCharacteristics(cameraIdList[0])
cameraManager.openCamera(cameraIdList[0], cameraStateCalback, null)
Log.d("JJJ", "start recording called")
}
这是要停止录制
override fun stopRecording() {
mediaRecorder.stop()
mediaRecorder.release()
Log.d("JJJ", "stop recording called")
}
最后,这就是一切的开始
videoCameraRecorder = VideoCameraRecorder(applicationContext)
Handler().postDelayed({
videoCameraRecorder.startRecording( object : VideoCameraCallback {
override fun onFinishedRecordingSuccess() {
Log.d(TAG, "onFinishedRecordingSuccess")
}
override fun onFinishedRecordingFailure() {
Log.d(TAG, "onFinishedRecordingFailure")
}
}, textureView)
Handler().postDelayed({
videoCameraRecorder.stopRecording()
}, 100000)
}, 5000)