How to use setCamera (MediaRecorder)?

Ignas Limanauskas picture Ignas Limanauskas · May 18, 2010 · Viewed 9.4k times · Source

According to Android SDK MediaRecorder.setCamera can be used to recycle the existing camera instance for video capture and preview without resetting the preview. I was not able to find any sample, and all my attempts were futile: I either get the wrong state exception, or MediaRecorder.prepare fails.

  • Does anyone know if this method usable at all?
  • How can I use it then?
  • Any samples available on the Web?

For reference: http://developer.android.com/reference/android/media/MediaRecorder.html#setCamera(android.hardware.Camera)

Answer

lyron picture lyron · Aug 19, 2013

I ran into the same problem and found out how it can work. Some things have to be done correctly. First you should check the state chart from the android document.

A working order of commands is as follows.

mCamera = Camera.open();
rec = new MediaRecorder();                               // state "Initial"

mCamera.lock();
mCamera.unlock();

rec.setCamera(mCamera);                                  // state still "Initial"
rec.setVideoSource(MediaRecorder.VideoSource.CAMERA);    // state "Initialized"
rec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);  // state "DataSourceConfigured"
rec.setVideoEncoder(MediaRecorder.VideoEncoder.H264);

rec.setPreviewDisplay(surfaceHolder.getSurface());

rec.setOutputFile(Environment.getExternalStorageDirectory() + "/test.mp4");

rec.prepare();                                           // state "Prepared"
rec.start();                                             // state "Recording"

// ...

rec.stop();                                              // state "Initial"

A full example can be found here.