'Video recording with cameraX 1.0.0-beta11 not working

I am trying to record video with latest version of cameraX 1.0.0-beta11 but I am facing issue in recording it.

Code snippet to start camera and record video.

@SuppressLint("RestrictedApi")
    private fun startCameraX() {
        val cameraProviderFuture = ProcessCameraProvider.getInstance(activity!!)

        cameraProviderFuture.addListener(Runnable {
            // Used to bind the lifecycle of cameras to the lifecycle owner
            val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()

            // Preview
            val preview = Preview.Builder()
                .build()
                .also {
                    it.setSurfaceProvider(previewView.surfaceProvider)
                }

              val videoCapture=VideoCapture.Builder().setVideoFrameRate(15).build()


            val videoFile = File(
                getOutputDirectory(),
                SimpleDateFormat(FILENAME_FORMAT, Locale.US
                ).format(System.currentTimeMillis()) + ".mp4")
            val outputOptions = VideoCapture.OutputFileOptions.Builder(videoFile).build()
            videoCapture.startRecording(outputOptions, ContextCompat.getMainExecutor(activity), object: VideoCapture.OnVideoSavedCallback{
                override fun onVideoSaved(outputFileResults: VideoCapture.OutputFileResults) {
                    Log.e("data","onVideoSaved")
                }

                override fun onError(videoCaptureError: Int, message: String, cause: Throwable?) {
                    Log.e("data", "onError->$message")
                }
            })

            // Select back camera as a default
            val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA

            try {
                // Unbind use cases before rebinding
                cameraProvider.unbindAll()

                // Bind use cases to camera
                cameraProvider.bindToLifecycle(
                    (this as LifecycleOwner), cameraSelector, preview, videoCapture)

            } catch(exc: Exception) {
                Log.e("TAG", "Use case binding failed", exc)
            }


        }, ContextCompat.getMainExecutor(activity))
    }

Output directory

private fun getOutputDirectory(): File {
            val mediaDir = activity?.externalMediaDirs?.firstOrNull()?.let {
                File(it, resources.getString(R.string.app_name)).apply { mkdirs() } }
            return if (mediaDir != null && mediaDir.exists())
                mediaDir else activity!!.filesDir
        }

Error - Not bound to a Camera [androidx.camera.core.VideoCapture@4bd02a4]

Please help!! Thanks in advance.



Solution 1:[1]

You must start the camera before calling the startRecording function. Basically you should output the following code in a function.

val videoFile = File(
    getOutputDirectory(),
    SimpleDateFormat(FILENAME_FORMAT, Locale.US
).format(System.currentTimeMillis()) + ".mp4")
val outputOptions = VideoCapture.OutputFileOptions.Builder(videoFile).build()
videoCapture.startRecording(outputOptions, ContextCompat.getMainExecutor(activity), object: VideoCapture.OnVideoSavedCallback{
    override fun onVideoSaved(outputFileResults: VideoCapture.OutputFileResults) {
        Log.e("data","onVideoSaved")
    }
    override fun onError(videoCaptureError: Int, message: String, cause: Throwable?) {
        Log.e("data", "onError->$message")
    }
})

And call it when the user clicks record button, then, when user clicks stop button you can stop recording with

videoCapture.stopRecording()

Once stopRecording function is called, if everything is ok, onVideoSaved function is gonna be called

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tyler2P