Search code examples
androidandroid-camera2mediarecorderandroid-mediarecorderandroid-video-record

Use both back and front camera for the same MediaRecorder session Camera2API


I need to make a video capturer which records something and then saves the file to the disk.

I succeeded in making it work with either back camera and front camera separately but i can't figure out how to make them work together and switch between them the same way the native app does while recording.

I've read that back and front camera are separate devices but I was wondering if there is a possibility to make them work together or the only thing I can do is that when I switch between the cameras to save the current file, start a new media recorder session and record another file and to merge them after I finish.

Thanks


Solution

  • On many modern Android phones the two cameras can work in parallel. You cannot use MediaRecorder to work with such stream, because it can either work with input from one camera (and you have no control over the recording until you stop it), or record a Surface.

    Well, in principle you could draw the frames from the two cameras on the same Surface and connect this Surface to a MediaRecorder, but it would not work smoothly.

    What you can do, is receive the frames from the two cameras, merge them together to receive a composite frame, and pass these frames to MediaCodec, and then – to MediaMuxer, similar to the camera recording example.