title: Video recording - unable to start camera preview
body: <p>I'm working on a custom video recording class, and I'm having some issues getting the camera preview to display when the Activity first appears. I'm calling this function inside the <code>surfaceCreated</code> callback:</p>
  
  <pre><code>private void initRecorder(Surface surface) throws IOException {
  // It is very important to unlock the camera before doing setCamera
  // or it will results in a black preview
  if(camera == null) {
      camera = Camera.open();
      camera.unlock();
  }
  
  if(recorder == null)
      recorder = new MediaRecorder();
  
  recorder.setPreviewDisplay(surface);
  recorder.setCamera(camera);
  
  camera.startPreview();
  
  recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
  recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
  recorder.setOutputFile("/sdcard/test.mp4");
  recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
  recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
  recorder.setVideoEncodingBitRate(15000000);
  recorder.setMaxDuration(10000); // length of video in MS
  recorder.setVideoSize(720, 480);
  recorder.setVideoFrameRate(30);
  
  
  try {
      recorder.prepare();
  } catch (IllegalStateException e) {
      // This is thrown if the previous calls are not called with the 
      // proper order
      e.printStackTrace();
  }
  }
  </code></pre>
  
  <p>When the Activity starts, my app crashes saying:</p>
  
  <p><code>java.lang.RuntimeException: startPreview failed</code></p>
  
  <p>Above that error, I noticed a line saying:</p>
  
  <p><code>attempt to use a locked camera from a different process (old pid 4894, new pid 6405)</code></p>
  
  <p>When I step through the code, that error is occurring on the <code>camera.startPreview()</code> line. If I remove that line from my code, the preview shows up fine after I call <code>recorder.start()</code>, and prior to that I just have a black screen with my record button. Once I stop recording, the preview continues to show fine (I am calling <code>camera.startPreview()</code> after I stop recording).</p>
  
  <p>Since I'm calling <code>camera.unlock()</code> only a few lines prior to starting the preview, and the two calls occur in the same function, how can I be having this error? </p>
  
  <p><strong>Edit</strong>: I tested the same code minus the call to <code>startPreview()</code> on a Droid X2 and a Droid 1, and it works fine. It looks like the EVO 4G is the problem. I will continue to investigate.</p>