Seamlessly Switching Camera Lenses During Video Recording with CameraX on Android.

Ngenge Senior
5 min readSep 9, 2023
Photo by Angela Compagnone on Unsplash

One of the features of Snapchat that I have always been thinking about and trying to understand is how the app is able to switch between the front and back camera lenses during an ongoing recording.

One possible implementation that was suggested was to temporarily save a record when the lens is switched and at the final recording, join all the videos involved into a single video and delete the individual videos. This obviously sounds like a good plan but I guess you just want to switch between both lenses as a developer without all this hassle. Let us go.

1. Create a Project in Android Studio

Start by creating a new project in Android Studio and add the CameraX dependencies of at least version 1.3.0-beta01but in this demonstration, I use the version 1.3.0-rc01.

dependencies {
// CameraX dependencies
def camerax_version = "1.3.0-rc01"
implementation "androidx.camera:camera-core:${camerax_version}"
implementation "androidx.camera:camera-camera2:${camerax_version}"
implementation "androidx.camera:camera-lifecycle:${camerax_version}"
implementation "androidx.camera:camera-video:${camerax_version}"
implementation "androidx.camera:camera-view:${camerax_version}"
implementation…

--

--