Android视频录制器:无法获得表面

时间:2017-11-30 21:34:01

标签: android video camera kotlin android-things

您好我正在尝试使用相机v2 API使用连接到它的Raspberry Pi相机模块的Raspberry Pi 3设备录制视频。

我正在使用Kotlin的Android Things开发它。

以下是我的视频录制代码的样子。

  override fun startRecording(videoCameraCallback: VideoCameraCallback) {
    val cameraIdList = cameraManager.cameraIdList
    cameraManager.openCamera(cameraIdList[0], cameraStateCalback, null)

    Log.d("JJJ", "start recording called")
}

    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()
            val surfaces = ArrayList<Surface>()
            surfaces.add(mediaRecorderSurface)
            captureRequest =  camera.createCaptureRequest(TEMPLATE_RECORD).build()
            camera.createCaptureSession(surfaces, cameraCaptureSessionCallBack,handler)
            Log.d("JJJ", "Created thread handler and capture session")
        }
    }

    override fun onDisconnected(camera: CameraDevice?) {
        Log.d("JJJ", "on disconnected")
    }

    override fun onError(camera: CameraDevice?, error: Int) {
        Log.d("JJJ", "on error")
    }
}



 var  cameraCaptureSessionCallBack = object :  CameraCaptureSession.StateCallback(){

    override fun onConfigureFailed(session: CameraCaptureSession?) {
        Log.d("JJJ", "on configured failed")
    }

    override fun onConfigured(session: CameraCaptureSession?) {
        Log.d("JJJ", "start recording")
        mediaRecorder.start()

    }
}


    private fun setMediaOutputSurface(): Surface {
          var file = File(context.filesDir.absolutePath)
        if(file.canWrite() && file.canRead()){
            Log.d("JJJ", "File location is fine")
        }

        mediaRecorder = MediaRecorder()
        mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE) //changed this to surface from camera and it fixed the cant get surface error

        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
        mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264)
        mediaRecorder.setVideoSize(600,600)


//        mediaRecorder.setVideoEncodingBitRate(10000000)
//        mediaRecorder.setVideoEncodingBitRate(512 * 1000)
        mediaRecorder.setOutputFile(context.filesDir.absolutePath + "/"+System.currentTimeMillis() + ".mp4")
        mediaRecorder.setVideoFrameRate(30)

        mediaRecorder.prepare()
//        mediaRecorder.start()
        return mediaRecorder.surface

}

这是我的策略

  1. 使用相机管理器获取相机ID列表。
  2. 从列表中打开第一台摄像机(只连接一台摄像机
  3. 成功打开后,创建处理程序线程
  4. 使用所有必要参数(视频源,帧速率等)
  5. 创建mediaRecorder
  6. mediaRecorder(失败的地方)
  7. 获取表面对象
  8. 使用媒体播放器和先前创建的hanlder的表面创建捕获请求
  9. 配置成功后,请致电mediaRecorder.start开始录制视频。
  10. 我得到的完整堆栈跟踪错误如下:

      12-01 09:58:23.981 8776-8776/com.jr.survailancedropboxcam W/CameraDevice-JV-0: Stream configuration failed due to: endConfigure:372: Camera 0: Unsupported set of inputs/outputs provided
    12-01 09:58:23.985 8776-8958/com.jr.survailancedropboxcam D/JJJ: on configured failed
    12-01 09:58:23.985 8776-8776/com.jr.survailancedropboxcam E/CameraCaptureSession: Session 0: Failed to create capture session; configuration failed
    

    提前致谢

1 个答案:

答案 0 :(得分:3)

使用最新版本(AndroidThings DP 6),您可以使用Camera v2 API

https://android-developers.googleblog.com/2017/11/android-things-developer-preview-6.html

这应该有效:https://github.com/googlesamples/android-Camera2Basic

您需要确保清单

中的标志设置为true
<activity

    ...
    android:hardwareAccelerated="true">
  

使用Camera2 API和TextureView的Camera2Basic示例现在可以在基于NXP和Raspberry Pi的设备上运行(hardwareAccelerated标志设置为true)