5

I want to save the video every 5 seconds while the video recording is ON.

I have tried many solutions but I am facing a Glitch that is, the Last Saved Frame remains in preview for around 300ms.

I think the reason is in MediaRecorder class "Once a recorder has been stopped, it will need to be completely reconfigured and prepared before being restarted."

Thanks

Abhishek
  • 2,350
  • 2
  • 21
  • 35

2 Answers2

3

I think it's impossible to do that with MediaRecorder. The better approach could be encoding video by using MediaCodec and storing encoded content bt using MediaMuxer.

Grafika is a project on Google Github account which is a dumping ground for Android graphics & media hacks. In this project, you can find good examples of using both MediaCodec and MediaMuxer classes.

I forked the Grafika project and did some modifications to support sequential segmented recording. You can find it here. When you run the application, select Show + capture camera item from the list and then set Output Segment Duration for example to 5 and then press Start recording button.

Please look at VideoEncoderCore and CameraCaptureActivity classes source code to find how it works. You can find here how it segments live camera feed to different files.

Mir Milad Hosseiny
  • 2,769
  • 15
  • 19
0

"I think the reason is in MediaRecorder class, "Once a recorder has been stopped, it will need to be completely reconfigured and prepared before being restarted"."

You can use multiple mediaMuxer's to encode separate files.

  • The camera should send data to fill a MediaMuxer object (which itself produces an .mp4 file).

  • When needed, you can start writing the Camera data to a second (different) MediaMuxer thus automatically creating a second new .mp4 file (on begin usage of the muxer).

  • The first MediaMuxer can then close and save its file. Your first segment is ready...

If needed, try to study this code for a guide on using Camera with mediaMuxer:
https://bigflake.com/mediacodec/CameraToMpegTest.java.txt

So you have a function that handles things when the 5 second interval has passed? In that function, could cycle the recording between two muxers, giving one a chance to close its file, while the other records the next segment and then vice-versa).

Instead of something like below (using MediaRecorder.OutputFormat.MPEG_4):

this.mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

You will instead create a new muxer (with MUXER_OUTPUT_MPEG_4):

//# create a new File to ssave into
File outputFile = new File(OUTPUT_FILENAME_DIR, "/yourFolder/Segment" + "-" + mySegmentNum + ".mp4");
String outputPath = outputFile.toString();
int format = MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4;

try { mMuxer = new MediaMuxer(outputPath, format); } 
catch (IOException e) { Log.e(TAG, e.getLocalizedMessage()); }

And you stop a muxer with:

mMuxer1.stop(); mMuxer1.release();

PS:

Another option is to use Threads to run multiple MediaRecorders. It might help your situation.
See the Android Background Process guide.

VC.One
  • 14,790
  • 4
  • 25
  • 57