title: Screen recording in android using MediaRecorder api
body: <p>Hey I'm trying to record a screencast app in android lolipop using mediarecorder api. Problem is that my application acts weird. Whenever I call start recording on the VideoRecorder class even when hardcoding the configuration like video size and output file the phone reboots. The app previosuly worked fine - saved the stuff in the correct place and the video itself looked good but then I've changed something in the code and now it doesn't work. Any idea what I'm missing?
  Here is my code:</p>
  
  <pre><code>public class VideoRecorder {
  private int screenDensity, screenHeight, screenWidth;
  private MediaRecorder mMediaRecorder;
  private VirtualDisplay mVirtualDisplay;
  private MediaProjection mediaProjection;
  private String directory, filename;
  private Display defaultDisplay = null;
  private DisplayMetrics metrics;
  
  public VideoRecorder(Display defaultDisplay) {
      //this.defaultDisplay = defaultDisplay;
      //metrics = new DisplayMetrics();
      //defaultDisplay.getMetrics(metrics);
      //screenDensity = metrics.densityDpi;
     // screenHeight = metrics.heightPixels;
     // screenWidth = metrics.widthPixels;
  }
  
  void prepareVideoRecorder() {
      initRecorder();
      prepareRecorder();
  }
  
  private void initRecorder() {
      mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
      mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
      mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
      mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
      mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
      mMediaRecorder.setVideoEncodingBitRate(512 * 1000);
      mMediaRecorder.setVideoFrameRate(30);
      mMediaRecorder.setVideoSize(540, 888);
     // mMediaRecorder.setOutputFile(directory + "/" + filename + ".mp4");
       mMediaRecorder.setOutputFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getAbsoluteFile() + "/recorderrsr.mp4");
  
  }
  
  void startRecording(MediaProjection mediaProjection) {
      mMediaRecorder = new MediaRecorder();
      initRecorder();
      prepareRecorder();
      this.mediaProjection = mediaProjection;
      mMediaRecorder.start();
      mVirtualDisplay = createVirtualDisplay();
  }
  
  private VirtualDisplay createVirtualDisplay() {
      return mediaProjection.createVirtualDisplay("MainActivity",
              540, 888, 240,
              DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
              mMediaRecorder.getSurface(), null /*Callbacks*/, null /*Handler*/);
  }
  
  void setFilename(String filename) {
      this.filename = filename;
  }
  
  void setDirectory(String directory) {
      this.directory = directory;
  }
  
  void stopRecording() {
      mMediaRecorder.stop();
      mMediaRecorder.reset();
      if (mediaProjection != null) {
          mediaProjection.stop();
          mediaProjection = null;
      }
      if (mMediaRecorder != null) {
          mMediaRecorder.release();
          mMediaRecorder = null;
      }
      if (mVirtualDisplay != null) {
          mVirtualDisplay.release();
      }
  
  }
  
  
  private void prepareRecorder() {
      try {
          mMediaRecorder.prepare();
      } catch (IllegalStateException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  
  void release() {
      mMediaRecorder.release();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I'm sure that the location exists and I have the correct permissions - as I've said the app worked fine before.
  Thanks for help Jon</p>