MediaRecorder problems when starting video capture on Android

I am trying to develop an application that, among other things, can record video from its user. Therefore, I need to capture the video using the front camera, if any. I am creating a camera preview and this works great. I used Google Sites to create MediaRecorder and set it up. If I use CamcorderProfile, my Media Server dies when I call start (). If I configure the encoder myself, the media server throws an exception at run time at startup () with the message "start failed: -19" I found several questions on this topic, but no one solved my problem. I think this may be due to the fact that I am not using a reverse camera. Perhaps I did not find the documentary I needed to create the correct code. I think that this is not only my problem, and I would be happy to receive more information about using the camera. My code is:

onResume () where the preview is configured

protected void onResume() { super.onResume(); // 1. set up camera preview if(checkCameraHardware(this)){ mCamera = getCameraInstance(); mCameraPreview = new CameraPreview(this, mCamera); FrameLayout preview = (FrameLayout) findViewById(id.cameraPreview); preview.addView(mCameraPreview); } else{ Log.d("Recorder", "camera check returned false"); } } 

method used checkCameraHardware ()

 private boolean checkCameraHardware(Context context){ boolean ret = true; if(context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){ ret = true; } else { ret = false; } return ret; } 

and getCameraInstance () method

 public static Camera getCameraInstance(){ Camera c = null; int cnum = 0; mCamSelect = 0; Camera.CameraInfo caminfo = new CameraInfo(); try { cnum = Camera.getNumberOfCameras(); Log.d("getCameraInstance", String.valueOf(cnum)); for(int i = 0;i<cnum;i++){ Camera.getCameraInfo(i, caminfo); if(caminfo.facing == CameraInfo.CAMERA_FACING_FRONT){ mCamSelect = i; break; } } c = Camera.open(mCamSelect); // attempt to get a Camera instance } catch (Exception e){ Log.d("getCameraInstance", "FATAL camera could not be opened"); // Camera is not available (in use or does not exist) } if(c==null)Log.d("getCameraInstance", "no camera returned"); return c; // returns null if camera is unavailable } 

this piece of code shows where the error appears (inside the onClick callback)

 if(prepareVideoRecorder()){ mMediaRecorder.start(); //here the errors occure recording = true; //start recording } 

and three MediaRecorder methods: prepareVideoRecorder (), releaseMediaRecorder () and release Camera ()

 private void releaseMediaRecorder(){ if (mMediaRecorder != null) { mMediaRecorder.reset(); // clear recorder configuration mMediaRecorder.release(); // release the recorder object mMediaRecorder = null; mCamera.lock(); // lock camera for later use } } private void releaseCamera(){ if (mCamera != null){ mCamera.release(); // release the camera for other applications mCamera = null; } } private boolean prepareVideoRecorder(){ //ex: mCamera = getCameraInstance(); mMediaRecorder = new MediaRecorder(); // Step 1: Unlock and set camera to MediaRecorder mCamera.unlock(); mMediaRecorder.setCamera(mCamera); // Step 2: Set sources mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); // Step 3: Set a CamcorderProfile (requires API Level 8 or higher) CamcorderProfile profile = CamcorderProfile.get(mCamSelect, CamcorderProfile.QUALITY_HIGH); if(profile == null){Log.d(tag, "the camcorder profile instance is null"); mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP); }else{ mMediaRecorder.setProfile(profile); } // Step 4: Set output file //ex: mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString()); mMediaRecorder.setOutputFile(currentVidFile.getAbsolutePath()); // Step 5: Set the preview output mMediaRecorder.setPreviewDisplay(mSlideview.getHolder().getSurface()); // Step 6: Prepare configured MediaRecorder try { mMediaRecorder.prepare(); } catch (IllegalStateException e) { Log.d(tag, "IllegalStateException preparing MediaRecorder: " + e.getMessage()); releaseMediaRecorder(); return false; } catch (IOException e) { Log.d(tag, "IOException preparing MediaRecorder: " + e.getMessage()); releaseMediaRecorder(); return false; } return true; } public void onGesturePerformed(GestureOverlayView arg0, Gesture arg1) { // TODO Auto-generated method stub } 

}

+4
source share
2 answers

I answer my question to help everyone who has the same problem. The mistake was so stupid that it was a little awkward to admit it.

In preparing the audio and video sources, I made the wrong surface.

I have different SurfaceViews and the surface of the wrong SurfaceView is being transferred to MediaRecorder. This led to an attempt to connect two different sources to the surface, which is impossible, and leads to the disconnection of Media Server.

I tested my application on GalaxyPad 10.1 and the video is working fine. I tested the application on VM Dalvik, and the video was black and white, but it also works.

Hope this helps.

+4
source

Have you added the following permissions to the manifest?

 <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> 
+1
source

Source: https://habr.com/ru/post/1434803/


All Articles