How to create mediaRecorder for video and connect to existing textureView with Camera2 Kotlin API

88 views Asked by At

I have a textureView initialized by:

@SuppressLint("MissingPermission")
    fun open_camera() {
        cameraManager.openCamera(camIdWithFlash, object: CameraDevice.StateCallback() {
            override fun onOpened(camera: CameraDevice) {
                cameraDevice = camera

                capReq = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW)
                var surface = Surface(textureView.surfaceTexture)
                capReq.addTarget(surface)

//                captureRequest = capReq.build()


                cameraDevice.createCaptureSession(listOf(surface), object:
                    CameraCaptureSession.StateCallback() {
                    override fun onConfigured(session: CameraCaptureSession) {
                        cameraCaptureSession = session
                        // Setting up capture request modifications
//                        capReq.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.FLASH_MODE_OFF)

                        cameraCaptureSession.setRepeatingRequest(capReq.build(), null, null)
                    }

                    override fun onConfigureFailed(session: CameraCaptureSession) {
                        TODO("Not yet implemented")
                    }

                }, handler)
            }

            override fun onDisconnected(camera: CameraDevice) {

            }

            override fun onError(camera: CameraDevice, error: Int) {

            }

        }, handler)
    }

However I am really lost in how to create a mediaRecorder to take video and connect it with the texture view. I've tried watching tutorials but they are often really old and do not work. Any help would be appreciated. Thank you!

1

There are 1 answers

3
Darian-Cătălin Cucer On

Declare specific variables in activity and fragment.

private var mediaRecorder: MediaRecorder? = null
private lateinit var videoFilePath: String

Initialize MediaRecorder.

private fun setUpMediaRecorder() {
    //mediaRecorder = MediaRecorder()
    mediaRecorder?.apply? {

        //setVideoSource
        //setOutputFormat
        //setOutputFile
        //setVideoEncodingBitRate
        //setVideoFrameRate
        //setVideoSize
        //setVideoEncoder

        //prepare()

    }
}

Use a similar guide to record video and connect to TextureView using Camera2 Kotlin Api
  1. Instance the CameraManager and TextureView.
  2. Check camera permissions.
  3. Use CameraManager and set TextureView.
  4. Instance MediaRecorder and init parameters using initMediaRecorder.
  5. Create Surface for MediaRecorder and add it as target for CaptureRequest.
  6. Declare the fun.
  7. Call startRecording and stopRecording functions.

The function has to set up MediaRecorder. getRotationCompensation is a helper function, and you should define it like this:

private fun getRotationCompensation(cameraId: String, activity: Activity): Int {
    //val rotation = activity. ...
    //val sensorOrientation = getSensorOrientation

    return if (isFrontCamera(cameraId)) {
        (sensorOrientation + rotation) % 360
    } else {  // back-facing camera
        (sensorOrientation - rotation + 360) % 360
    }
}

private fun getSensorOrientation(cameraId: String): Int {
    //val characteristics = cameraManager. ...
    //return characteristics. ...
}

private fun isFrontCamera(cameraId: String): Boolean {
    //val characteristics = cameraManager. ...
    //val facing = characteristics. ...
    //return facing == CameraCharacteristics. ...
}

Create Recording function that has to start the MediaRecorder and should begin recording.

private fun startRecording() {
    if (mediaRecorder == null) {
        //setUpMediaRecorder()
    }

    //mediaRecorder?.start()
}

Create Stop Recording function that has to stop the MediaRecorder and reset the recording.

private fun stopRecording() {
    //mediaRecorder?.apply {
        //stop()
        //reset()
    }
}

Create open_camera fun to include MediaRecorder Callback.

    @SuppressLint("...permisions")
    fun open_camera() {
        cameraManager...(..., object : ...() {
            override fun onOpened(camera: CameraDevice) {
                cameraDevice = camera
    
                //capReq = cameraDevice...(...camera device)
                //var surface = Surface(...)
                //capReq.addTarget(surface)
    
                // Set up MediaRecorder
                //setUpMediaRecorder()
    
                cameraDevice.createCaptureSession(
                    listOf(..., ...),
                    //Create object of CameraCaptureSession
    
                            override fun onDisconnected(camera: CameraDevice) {
                        //stopRecording()
                    }
            }, handler)
        }
    }
    }

Create a function to get file path:

private fun getVideoFilePath(): String {
    //val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date())
    //val fileName = ...
    //val storageDir = ...
    return "${storageDir?.absolutePath}/$fileName"
}

Call startRecording fun and call stopRecording fun:

startRecording()
...
stopRecording()