title: using media projection with media recorder produce corrupted video
body: <p>I am trying to use media projection api that became available on lollipop devices (android 5 and above), now I realized 2 ways of using this api with android:</p>
  
  <p><strong>First way:</strong></p>
  
  <p>Using the mediacodec and media muxer.</p>
  
  <p><strong>Second way</strong></p>
  
  <p>Using the media recorder class.</p>
  
  <p><strong>What I did:</strong></p>
  
  <p>I used first media codec and it worked fine on different devices , but the problem came when I needed to add audio (it seemed a bit complicated).</p>
  
  <p>Then I tried to use media recorder as some others used it and seemed simple to add audio, so I did something like this:</p>
  
  <pre><code>//set a media recorder
  try{
  MediaRecorder mediarecorder = new MediaRecorder();
  mediarecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  mediarecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
  mediarecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
  mediarecorder.setOutputFile(path_file);
  mediarecorder.setVideoSize(480, 270);
  mediarecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
  mediarecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
  mediarecorder.setVideoEncodingBitRate(70000);
  mediarecorder.setVideoFrameRate(30);
  
  mediarecorder.prepare();
  
  }catch(IOException e){
  
  
  }
  
  //density
  Metrics metrics = getReources().getDisplayMetrics();
  int density = metrics.densityDpi;
  
  //set virtual display
  
  media_projection.createVirtualDisplay("name",480,270,denisty,DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mediarecorder.getSurface(),null,null);
  
  //start
  mediarecorder.start();
  </code></pre>
  
  <p>I am hard coding: <code>width</code>, <code>height</code>, <code>frame rate</code> and <code>bitrate</code> because I need the output video to be controlled, beacause videos tend to get large in size.</p>
  
  <p><strong>My problem:</strong></p>
  
  <p>Using the media recorder method, I realized a problem while testing the api, I used 2 different devices (android 7 and android 6) both are different interms of screen density but approximately they are of same size:</p>
  
  <p><strong>Android 7 device(resolution: 720x1280):</strong></p>
  
  <p>I got perfect results no problem (this device is the higher resolution device).</p>
  
  <p><strong>Android 6 device(resolution: 540x960):</strong></p>
  
  <p>I got a weird corrupted output video, such that if I recorder a white screen i get corrupted video with purple blurry stuff, and if I recorded a blue screen I get a corrupted video with green blurry stuff.</p>
  
  <p><strong>Question:</strong></p>
  
  <p>While these don't occur using media codec, my question is:</p>
  
  <p>why this occurs using media recorder?</p>
  
  <p>Is media recorder stable to use with media projection?</p>
  
  <p>Thanks.</p>