Search code examples
androidsurfaceviewandroid-camera2android-mediacodecvideo-recording

Switch Camera (Back to front and Front to back) in Android while recoding the video - Camera2


What I am looking for

  • How to switch the camera from front to back (or back to front) while video is recording on Android app
  • Should use camera2 APIs (No deprecated API from old camera API)

What I have tried so far

  • With MediaRecorder, I am able to pause the recorder while I switch the cameras and resume when switched camera is opened.
    • Issues with this one is that i am not able to reconfigure the orientation as the recorder is already prepared and in resume state.
    • I have tried the code from this FlipCam repo code and it works on the lower android version but does not work on latest ones. (It uses deprecated camera API and the code it too complex to understand)

From the FlipCam repo I can see that they are trying to render the camera output on the GLSurfaceView and when we start the recording they copy the frames using from surface view to MediaRecorder's surface using OpenGL.

Now, OpenGL is very low level API and i would rather go with some high level APIs where there is less maintenance.

Also there is an approach where we just save the temporary part of videos and then merge at the end. But doing so would take some time to process and I would like the output to be almost instant when user stops the recording.

Would this be possible with MediaCodec and MediaMuxer? Please share your thoughts or provide any guiding path to implement this using latest and comparatively high level Android APIs.


Solution

  • The Jetpack CameraX library just added support for this capability in its 1.3.0-rc01 release.

    Even if you don't want to adopt the library, you could see how it added this capability. I'm not sure MediaRecorder can handle this, but MediaCodec certainly can, most likely using persistent surfaces.