title: Android Camera recording video but plays upside down
body: <p>I record a video using the below code and it records perfectly, but when it plays the video, it plays it upside down.</p>
  
  <p>I tried settings <code>mrec.setOrientationHint(180)</code> before <code>mrec.prepare();</code> but it was useless. Any hints?</p>
  
  <pre><code>import java.io.File;
  import java.io.IOException;
  import java.lang.reflect.InvocationTargetException;
  import java.lang.reflect.Method;
  
  import android.app.Activity;
  import android.hardware.Camera;
  import android.media.MediaRecorder;
  import android.os.Bundle;
  import android.os.Environment;
  import android.util.Log;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.Surface;
  import android.view.SurfaceHolder;
  import android.view.SurfaceView;
  import android.view.Window;
  
  /**
   * @author SANA HASSAN
   */
  public class CameraSurfaceView extends Activity {
  
      private Preview mPreview;
      private MediaRecorder mrec = new MediaRecorder();
      private int cameraId = 0;
      private Camera mCamera;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          requestWindowFeature(Window.FEATURE_NO_TITLE);
  
          mPreview = new Preview(this);
          setContentView(mPreview);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          menu.add(0, 0, 0, "Start");
          menu.add(0, 1, 0, "Stop");
          return super.onCreateOptionsMenu(menu);
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
              case 0:
                  try {
                      startRecording();
                  } 
                  catch (Exception e) {
                      e.printStackTrace();
                      mrec.release();
                  }
                  break;
  
              case 1: 
                  mrec.stop();
                  mrec.release();
                  mrec = null;
                  break;
  
              default:
                  break;
          }
          return super.onOptionsItemSelected(item);
      }
  
      protected void startRecording() throws IOException  {
  
          mrec = new MediaRecorder();
          mrec.setCamera(mCamera);
          mCamera.unlock();
          File directory = new File(Environment.getExternalStorageDirectory()+"/NICUVideos");
          directory.mkdirs();
          mrec.setAudioSource( MediaRecorder.AudioSource.MIC);
          mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
          mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
          mrec.setOutputFile(Environment.getExternalStorageDirectory()+"/NICUVideos/"+System.currentTimeMillis()+".mp4"); 
          mrec.setPreviewDisplay(mPreview.getHolder().getSurface());
          mrec.setVideoSize(640, 480);
  
          Method[] methods = mrec.getClass().getMethods();
          for (Method method: methods){
              try{
                  if(method.getName().equals("setAudioEncodingBitRate")){
                      method.invoke(mrec, 12200);
                  }
                  else if(method.getName().equals("setVideoEncodingBitRate")){
                      method.invoke(mrec, 800000);
                  }
                  else if(method.getName().equals("setAudioSamplingRate")){
                      method.invoke(mrec, 8000);
                  }
                  else if(method.getName().equals("setVideoFrameRate")){
                      method.invoke(mrec, 20);
                  }
              }
              catch (IllegalArgumentException e) {
                  e.printStackTrace();
              } 
              catch (IllegalAccessException e) {
                  e.printStackTrace();
              } 
              catch (InvocationTargetException e) {
                  e.printStackTrace();
              }
          }
          mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
          mrec.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
          mrec.setMaxDuration(60000); // 60 seconds
          mrec.setMaxFileSize(10000000); // Approximately 10 megabytes
          mrec.prepare();
          mrec.start();
      }
  
      protected void stopRecording() {
          mrec.stop();
          mrec.release();
          mCamera.release();
      }
  
      class Preview extends SurfaceView implements SurfaceHolder.Callback {
          SurfaceHolder mHolder;
          Activity activity;
  
          Preview(Activity activity) {
              super(activity);
              mHolder = getHolder();
              mHolder.addCallback(this);
              mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
          }
  
          public void surfaceCreated(SurfaceHolder holder) {
  
              Camera.CameraInfo info=new Camera.CameraInfo();
              for (int i=0; i &lt; Camera.getNumberOfCameras(); i++) {
                  Camera.getCameraInfo(i, info);
                  if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                      mCamera=Camera.open(i);
                      cameraId = i;
                  }
              }
  
              try {
                 mCamera.setPreviewDisplay(holder);
              } catch (IOException exception) {
                  mCamera.release();
                  mCamera = null;
              }
          }
  
          public void surfaceDestroyed(SurfaceHolder holder) {
              mCamera.stopPreview();
              mCamera.release();
              mCamera = null;
          }
  
          public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
              setCameraDisplayOrientation(mCamera);
              mCamera.startPreview();
          }
  
          public void setCameraDisplayOrientation(Camera camera) {
              Camera.CameraInfo info = new Camera.CameraInfo();
              Camera.getCameraInfo(cameraId, info);
  
              int rotation = CameraSurfaceView.this.getWindowManager().getDefaultDisplay().getRotation();
              int degrees = 0;
              switch (rotation) {
                  case Surface.ROTATION_0: degrees = 0; break;
                  case Surface.ROTATION_90: degrees = 90; break;
                  case Surface.ROTATION_180: degrees = 180; break;
                  case Surface.ROTATION_270: degrees = 270; break;
              }
  
              int result;
              if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                  result = (info.orientation + degrees) % 360;
                  result = (360 - result) % 360;  // compensate the mirror
              } else {  // back-facing
                  result = (info.orientation - degrees + 360) % 360;
              }
              Log.d(Vars.TAG, "Result = "+result);
              camera.setDisplayOrientation(result);
          }
      }
  
  }
  </code></pre>