How to use setCamera (MediaRecorder)?

According to the Android SDK, MediaRecorder.setCamera can be used to recycle an existing camera instance for video capture and preview without resetting the preview. I could not find any sample, and all my attempts were useless: I am either mistaken because of the state, or MediaRecorder.prepare does not work.

  • Does anyone know if this method can be used at all?
  • How can i use it?
  • Any samples available online?

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

+6
android camera mediarecorder
source share
6 answers

The best example is the source of an Android application for Android. After some investigation, I found that recorder.setCamera(camera) should be called immediately after creating an instance of MediaRecorder or at least before any settings are applied to it. Applying any parameters ( setVideoSource() , etc.) Before calling setCamera() , an error setCamera() .

+6
source share

I ran into the same problem and figured out how it might work. Some things need to be done correctly. First you have to check the status diagram from android doku link .

The working order of the teams 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 complete example can be found here .

+6
source share

Have you tried to use the following functions after creating the mediarecorder instance?

// Unlock the camera to allow another process to access it.

mCameraDevice.unlock ();

// Sets the camera to record. Use this function to quickly switch between preview and // capture modes without tearing off the camera object.

mMediaRecorder.setCamera (mCameraDevice);

+1
source share

I got a hint from @lyron.

First open the front camera.

  int cameraId = -1; int camNums = Camera.getNumberOfCameras(); for( int i = 0 ; i < camNums ; i++) { CameraInfo info = new CameraInfo(); Camera.getCameraInfo(i, info); if( info.facing == CameraInfo.CAMERA_FACING_FRONT ) { cameraId = i; break; } } mCamera = Camera.open(cameraId); mCamera.unlock(); 

I need to use the front camera as above.

AND DON'T FORGET to unlock the camera.

If you do not, you will see errors below.

 E/MediaRecorder(15509): start failed: -19 E/SampleVideoRecorder(15509): Exception : E/SampleVideoRecorder(15509): java.lang.RuntimeException: start failed. E/SampleVideoRecorder(15509): at android.media.MediaRecorder.start(Native Method) 

Second , install the camera before installing others like this.

  recorder = new MediaRecorder(); recorder.setCamera( mCamera ); // like this! recorder.setAudioSource( MediaRecorder.AudioSource.MIC); recorder.setVideoSource( MediaRecorder.VideoSource.CAMERA); recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP); recorder.setVideoSize( 2560, 1440 ); recorder.setVideoFrameRate(30); recorder.setPreviewDisplay(holder.getSurface()); recorder.setOutputFile( s_dir ); try { recorder.prepare(); recorder.start(); } catch( Exception e ) { Log.e("SampleVideoRecorder", "Exception : ", e ); recorder.release(); recorder = null; } 

Someone says that setCamera () should be called before preparing ().

But I see how my code works.

+1
source share

I had my MediaRecorder initiated:

 MediaRecorder mediaRecorder = null; 

but not like that:

 MediaRecorder mediaRecorder = new MediaRecorder(); 

(headbang) haha ​​.. now my problem is kalydascope to preview .. time to search interwebs ..

hope this helped someone.

0
source share

I found 2 links that may come in handy. Git repo Android camera and MediaRecorder example

-3
source share

All Articles