0

Back camera is working fine but,when we switch from back to front camera ,it crashes(in case of video recording using MediaRecorder ).... it showing error which i show in log !!

enter image description here

Here is my code :

private void start_work()
 {
  if(recording)
  {


   Log.v("LOGTAG", "Recording Stopped");


  }
  else
  {
   recording = true;
   progress_relative_lay.setVisibility(View.VISIBLE);
   button_capture.setVisibility(View.GONE);
   //show_icon();

   recorder.start();
   Log.v("LOGTAG", "Recording Started");


  }
 }

and for initialize i use

recorder = new MediaRecorder();


 recorder.setOrientationHint(result) ;
  recorder.setPreviewDisplay(holder.getSurface());
  if(usecamera)
  {
   camera.unlock();
   recorder.setCamera(camera);

  }
  recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
  recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
  recorder.setProfile(camcorderProfile);
Gagan
  • 745
  • 10
  • 31

2 Answers2

2
  if (camera != null) {
            throw new RuntimeException("camera already initialized");
        }

        Camera.CameraInfo info = new Camera.CameraInfo();

        // Try to find a front-facing camera (e.g. for videoconferencing).
        int numCameras = Camera.getNumberOfCameras();
        for (int i = 0; i < numCameras; i++) {
            Camera.getCameraInfo(i, info);
           //this will be front or back depending on the requirement 
            if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
                camera= Camera.open(i);
                break;
            }
        }

try adding this part of code

Preethi Rao
  • 5,117
  • 1
  • 16
  • 29
  • i am using the same---------------------- private Camera getCameraInstance(int type) { Camera c = null; int numberOfCameras = Camera.getNumberOfCameras(); for(int i = 0; i < numberOfCameras; i++) { CameraInfo info = new CameraInfo(); Camera.getCameraInfo(i, info); if(info.facing == type) { try { c = Camera.open(i); // attempt to get a Camera instance } catch(Exception e) { // Camera is not available Util_Class.show_Toast("Camera not available.",getActivity()); } break; } } return c; } – Gagan Apr 13 '15 at 06:41
  • Now i have make some changes and it is working fine but problem exist for recording --MediaRecorder.start(); in case of front camera......it throw error on this line – Gagan Apr 13 '15 at 06:51
  • code is almost same ,i just add if(camera != null) { camera.stopPreview(); camera.setPreviewCallback(null); camera.lock(); camera.release(); } camera.release was already there.. now i have error in recorder.start(); – Gagan Apr 13 '15 at 07:30
  • are you releasing the recorder ? – Preethi Rao Apr 13 '15 at 07:48
  • you cant have same recorder object for both front and back recording – Preethi Rao Apr 13 '15 at 07:54
  • not working.....i try by releasing recorder and then re initialize it but its not working – Gagan Apr 13 '15 at 08:03
  • put that part of code also.. i am not able to understand with this code ..its a part of code.. and error is coming from somewhere else – Preethi Rao Apr 13 '15 at 08:05
  • thats what i am trying to say.. i am not finding any recorder object in the above code – Preethi Rao Apr 13 '15 at 09:22
0

some times device go in onPause state ,i just re initialize everything in onResume method of the activity and it works..!

Gagan
  • 745
  • 10
  • 31