Skip to content

Recording video

Lucas Yuji edited this page Nov 6, 2022 · 1 revision

Recording Video

there's a little bit of difference between recording video and taking pictures with Camposer, but the methods to save are very similar with CameraState:

All of these options use the method CameraState.startRecording or CameraState.toggleRecording, which has the last parameter: the callback with video capture result, and if successful, it contains the saved URI.

addition: if you're using CameraState.startRecording, to stop video use CameraState.stopRecording. With CameraState.toggleRecording just call twice.

Here is an example using the file:

val cameraState = rememberCameraState()
val isRecording = rememberUpdateState(cameraState.isRecording) // check if it's recording

val context = LocalContext.current
CameraPreview(
  cameraState = cameraState,
) {
  Box(
    onClick = { 
      cameraState.toggleRecording(file) { result -> 
        if (result is VideoCaptureResult.Success) { // result.savedUri might be useful to you
          Toast.makeText(context, "Success!", Toast.LENGTH_LONG).show()
        }
      } 
    }
  ) {
    Text(text = "Record: $isRecording")
  }
}

There's also support to suspend functions! Using suspend functions TakePicture it's return savedUri or throw throwable, so be careful when used it.


CameraState.isRecording

Check if it's recording or not, can be used with rememberUpdateState or other states...


If you want to see some example using with androidx ViewModel, check out the Sample Project