title: Using android MediaRecorder
body: <p>Below is the structure of my working code to record video and audio:</p>
  
  <p>Questions:
  1) Why is the <code>CamcorderProfile</code> needed? <code>setProfile(...)</code> appears to set the dimensions to whatever QUALITY_HIGH gives, but later I set the dimensions I want with <code>setVideoSize(...)</code>, which overrides this. However, when I remove the two CamcorderProfile lines, the app crashes at <code>setVideoSize(...)</code> with LogCat <code>E/MediaRecorder(19526): setVideoSize called in an invalid state: 2</code>.</p>
  
  <p>2) How do I not record audio? The documentation states that if <code>setAudioSource(...)</code> is not called, there will be no audio track. However, when I remove that line the app crashes at <code>setProfile(...)</code> with LogCat <code>E/MediaRecorder(19946): try to set the audio encoder without setting the audio source first</code>.</p>
  
  <p>3) If I remove both CamcorderProfile lines and the <code>setAudioSource(...)</code> line, it crashes as in 1).</p>
  
  <p>4) I have also tried adding the line</p>
  
  <pre><code>recorder.setOutputFormat(OutputFormat.DEFAULT);
  </code></pre>
  
  <p>instead of the CamcorderProfile lines. But now it crashes at <code>perpare()</code>. If <code>setAudioSource(...)</code> is called the LogCat is: <code>E/MediaRecorder(20737): audio source is set, but audio encoder is not set</code> if it isn't called the LogCat is: <code>E/MediaRecorder(20544): video source is set, but video encoder is not set</code></p>
  
  <p>I have looked allover the internet and I can't find a good example of the correct way to setup the MediaRecorder. <a href="http://developer.android.com/guide/topics/media/camera.html" rel="nofollow noreferrer" title="Here">Here</a> it implies after API 8 you should use the CamcorderProfile class, but it seems to me that it is causing problems.</p>
  
  <p>Any help would be great! Thanks!</p>
  
  <p>Code (which works when run as it is below):</p>
  
  <pre><code>recorder = new MediaRecorder();
  recorder.setCamera(&lt;&lt;camera&gt;&gt;);
  recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
  recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
  
  CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
  recorder.setProfile(profile);
  
  recorder.setOutputFile(&lt;&lt;Path String&gt;&gt;);
  recorder.setVideoSize(&lt;&lt;Width&gt;&gt;, &lt;&lt;Height&gt;&gt;);
  
  recorder.setPreviewDisplay(&lt;&lt;Surface&gt;&gt;);
  
  recorder.setOrientationHint(0); 
  recorder.setMaxDuration(10000);
  recorder.setOnInfoListener(this);
  
  try
  {
      recorder.prepare();
      recorder.start();
  } catch ...
  </code></pre>