title: Surface view displays black screen for video preview
body: <p>Hi I am developing an android video app similar to this link
   <a href="http://android-er.blogspot.in/2011/10/video-capture-using-mediarecorder-with.html" rel="nofollow">http://android-er.blogspot.in/2011/10/video-capture-using-mediarecorder-with.html</a> 
  where in the video app is displayed in a frame layout.</p>
  
  <p>I am trying out the above one on a surface view and my xml looks like below.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:textSize="20sp" &gt;
  
  &lt;SurfaceView
      android:id="@+id/CameraView"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:focusable="true" /&gt;
  
  &lt;ImageView
      android:id="@+id/mybutton"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentRight="true"
      android:layout_alignParentTop="true"
      android:paddingLeft="2dp"
      android:paddingRight="2dp"
      android:src="@drawable/record_video" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>I am not sure why it displays the black screen instead of a camera view.  My ViewActivity.java is as below.</p>
  
  <pre><code>public class ViewActivity extends Activity implements OnClickListener, SurfaceHolder.Callback
  {
  MediaRecorder recorder;
  SurfaceHolder holder;
  boolean recording=false;
  private MediaRecorder mediaRecorder;
  private File file;
  private ImageView recVideo;
  private SurfaceView cameraView;
  private Camera camera;
  
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
      super.onCreate(savedInstanceState);
      requestWindowFeature(Window.FEATURE_NO_TITLE); 
  
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
  
      recorder = new MediaRecorder();
      initRecorder();
  
      setContentView(R.layout.view);
  
      recVideo = (ImageView)findViewById(R.id.mybutton);
      cameraView = (SurfaceView) findViewById(R.id.CameraView);
  
      holder = cameraView.getHolder();
      holder.addCallback(ViewActivity.this);
      holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  
      recVideo.setClickable(true);
      recVideo.setOnClickListener(this);
  }
  
  private void initRecorder() 
  {
      recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
      recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
      recorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
      recorder.setOutputFile(this.initFile().getAbsolutePath());
      recorder.setMaxDuration(60000); // Set max duration 60 sec.
      recorder.setMaxFileSize(5000000); // Set max file size 5M
  
  }
  
  private void prepareRecorder()
  {
      recorder.setPreviewDisplay(holder.getSurface());
  
      try 
      {
          recorder.prepare();
      } catch (IllegalStateException e) 
      {
          e.printStackTrace();
          finish();
      } 
      catch (IOException e) 
      {
          e.printStackTrace();
          finish();
      }
  }
  
  public void onClick(View v)
  {
      if (recording)
      {
          recorder.stop();
          recording = false;
  
          initRecorder();
          prepareRecorder();
          Toast.makeText(ViewActivity.this,"Video Saved!",Toast.LENGTH_SHORT).show();
          startActivity(new Intent(ViewActivity.this,ViewActivity.class));
          finish();
  
      }
      else 
      {
          recorder.start();
          recording = true;
          recVideo.setImageDrawable(getResources().getDrawable(R.drawable.stop_recording));
      }
  }
  
  public void surfaceCreated(SurfaceHolder holder) 
  {
      prepareRecorder();
  
  }
  
  public void surfaceChanged(SurfaceHolder holder, int format, int width,
          int height) 
  {
  }
  
  public void surfaceDestroyed(SurfaceHolder holder) 
  {
      if (recording) {
          recorder.stop();
          recording = false;
      }
      recorder.release();
      finish();
  }
  </code></pre>
  
  <p>I am not sure where I am going wrong. How to make the surface view display camera view instead of black screen.</p>
  
  <p>Please Help.</p>
  
  <p>Thanks!</p>