Learn how to get the recorded videos from the CameraResult
type using the Activity Result APIs.
Success
A Recording
has a duration
and contains a list of Video
s. The list contains either one Video
(for single camera recording) or two Video
s (for dual camera recordings).
Each Video
has:
- A
uri
to the video file that is stored inContext::getFilesDir()
. Make sure to copy the file to a permanent location if you want to access it later. - A
rect
that contains the position of the video as it was shown in the camera preview.
when (result) { is CameraResult.Record -> { for (recording in result.recordings) { Log.d(TAG, "Duration: ${recording.duration}") for (video in recording.videos) { Log.d(TAG, "Video Uri: ${video.uri} Video Rect: ${video.rect}") } } } is CameraResult.Reaction -> { Log.d(TAG, "Video uri: ${result.video.uri}") for (reaction in result.reaction) { Log.d(TAG, "Duration: ${reaction.duration}") for (video in reaction.videos) { Log.d(TAG, "Video Uri: ${video.uri} Video Rect: ${video.rect}") } } }
else -> { Log.d(TAG, "Unhandled result") } }
Standard Camera
If the user has recorded videos, the CameraResult.Record
case will contain a list of Recording
s, each representing a segment of the recorded video.
is CameraResult.Record -> { for (recording in result.recordings) { Log.d(TAG, "Duration: ${recording.duration}") for (video in recording.videos) { Log.d(TAG, "Video Uri: ${video.uri} Video Rect: ${video.rect}") } }}
Video Reaction
If the user has recorded a reaction, the CameraResult.Reaction
case will contain the video that was reacted to and a list of Recording
s, each representing a segment of the recorded video.
is CameraResult.Reaction -> { Log.d(TAG, "Video uri: ${result.video.uri}") for (reaction in result.reaction) { Log.d(TAG, "Duration: ${reaction.duration}") for (video in reaction.videos) { Log.d(TAG, "Video Uri: ${video.uri} Video Rect: ${video.rect}") } }}
Failure
The result is null
in case the user dismissed the camera at any point.
result ?: run { Log.d(TAG, "Camera dismissed") return@rememberLauncherForActivityResult}
Full Code
Here’s the full code:
import android.os.Bundleimport android.util.Logimport androidx.activity.compose.rememberLauncherForActivityResultimport androidx.activity.compose.setContentimport androidx.appcompat.app.AppCompatActivityimport androidx.compose.material3.Buttonimport androidx.compose.material3.Textimport ly.img.camera.core.CameraResultimport ly.img.camera.core.CaptureVideoimport ly.img.camera.core.EngineConfiguration
private const val TAG = "RecordingsCameraActivity"
class RecordingsCameraActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState)
val cameraInput = CaptureVideo.Input( engineConfiguration = EngineConfiguration( license = "<your license here>", userId = "<your unique user id>", ), )
setContent { val cameraLauncher = rememberLauncherForActivityResult(contract = CaptureVideo()) { result -> result ?: run { Log.d(TAG, "Camera dismissed") return@rememberLauncherForActivityResult } when (result) { is CameraResult.Record -> { for (recording in result.recordings) { Log.d(TAG, "Duration: ${recording.duration}") for (video in recording.videos) { Log.d(TAG, "Video Uri: ${video.uri} Video Rect: ${video.rect}") } } } is CameraResult.Reaction -> { Log.d(TAG, "Video uri: ${result.video.uri}") for (reaction in result.reaction) { Log.d(TAG, "Duration: ${reaction.duration}") for (video in reaction.videos) { Log.d(TAG, "Video Uri: ${video.uri} Video Rect: ${video.rect}") } } }
else -> { Log.d(TAG, "Unhandled result") } } }
Button( onClick = { cameraLauncher.launch(cameraInput) }, ) { Text(text = "Open Camera") } } }}