title: Video stretches horizontally while capturing
body: <p>I m trying to record a video but when i start recording it using media recorder it gets stretched horizontally.I have tried to applied lot of soltuion but nothing works for my code yet.
  This is what i have tried to do so far to resolve this issue. Can anyone suggest any solution.Thanks</p>
  
  <pre><code> public class MyCameraSurfaceView extends SurfaceView implements
              SurfaceHolder.Callback {
  
          private SurfaceHolder mHolder;
          private Camera mCamera;
  
          public MyCameraSurfaceView(Context context, Camera camera) {
              super(context);
              mCamera = camera;
  
              // Install a SurfaceHolder.Callback so we get notified when the
              // underlying surface is created and destroyed.
              mHolder = getHolder();
              mHolder.addCallback(this);
              // deprecated setting, but required on Android versions prior to 3.0
              mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
          }
  
          @Override
          public void surfaceChanged(SurfaceHolder holder, int format,
                                     int width, int height) {
              // If your preview can change or rotate, take care of those events
              // here.
              // Make sure to stop the preview before resizing or reformatting it.
              System.out.println("inside surfaceChanged");
              if (mHolder.getSurface() == null) {
                  return;
              }
              try {
                  mCamera.stopPreview();
              } catch (Exception e) {
                  e.printStackTrace();
              }
              try {
                  mCamera.setDisplayOrientation(90);
                  mCamera.setPreviewDisplay(mHolder);
  
                  Camera.Parameters parameters = mCamera.getParameters();
                  parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
                  mCamera.setParameters(parameters);
  
  ////////////////////////////////////////////////////////////
                  /*if (mCamera != null) {
                      mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();
                      Camera.Parameters parameters = mCamera.getParameters();
                      parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
                      mCamera.setParameters(parameters);
                  }*/
  
                  mCamera.startPreview();
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
  
          @Override
          public void surfaceCreated(SurfaceHolder holder) {
              // The Surface has been created, now tell the camera where to draw
              // the preview.
              System.out.println("inside surfaceCreated");
              try {
                  mCamera.setPreviewDisplay(holder);
                  mCamera.startPreview();
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
  
          @Override
          public void surfaceDestroyed(SurfaceHolder holder) {
  
          }
  
          public void setCamera(Camera camera) {
              // method to set a camera instance
              mCamera = camera;
          }
  
          public void refreshCamera(Camera camera) {
              if (mHolder.getSurface() == null) {
                  // preview surface does not exist
                  return;
              }
              // stop preview before making changes
              try {
                  mCamera.stopPreview();
              } catch (Exception e) {
                  // ignore: tried to stop a non-existent preview
              }
              // set preview size and make any resize, rotate or
              // reformatting changes here
              // start preview with new settings
              setCamera(camera);
              try {
                  mCamera.setDisplayOrientation(90);
                  mCamera.setPreviewDisplay(mHolder);
                  mCamera.startPreview();
              } catch (Exception e) {
                  Log.d(VIEW_LOG_TAG,
                          "Error starting camera preview: " + e.getMessage());
              }
          }
  
          @Override
          protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
              System.out.println("inside onMeasure");
              final int width = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec);
              final int height = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec);
              setMeasuredDimension(width, height);
              if (mSupportedPreviewSizes != null) {
                  mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width, height);
              }
          }
      }
  
      private Camera.Size getOptimalPreviewSize(List&lt;Camera.Size&gt; sizes, int w, int h) {
          final double ASPECT_TOLERANCE = 0.1;
          double targetRatio = (double) h / w;
  
          if (sizes == null) return null;
  
          Camera.Size optimalSize = null;
          double minDiff = Double.MAX_VALUE;
  
          int targetHeight = h;
  
          for (Camera.Size size : sizes) {
              double ratio = (double) size.width / size.height;
              if (Math.abs(ratio - targetRatio) &gt; ASPECT_TOLERANCE) continue;
              if (Math.abs(size.height - targetHeight) &lt; minDiff) {
                  optimalSize = size;
                  minDiff = Math.abs(size.height - targetHeight);
              }
          }
  
          if (optimalSize == null) {
              minDiff = Double.MAX_VALUE;
              for (Camera.Size size : sizes) {
                  if (Math.abs(size.height - targetHeight) &lt; minDiff) {
                      optimalSize = size;
                      minDiff = Math.abs(size.height - targetHeight);
                  }
              }
          }
          return optimalSize;
      }
  
  
   private boolean prepareMediaRecorder() {
          try {
              myCamera = getCameraInstance();
              setCameraDisplayOrientation(VideoRecordingActivity.this, cameraId, myCamera);
              myCamera.unlock();
              mediaRecorder = new MediaRecorder();
              mediaRecorder.setCamera(myCamera);
              mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
              mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
              if (cameraId == 1) {
                  mediaRecorder.setProfile(CamcorderProfile
                          .get(CamcorderProfile.QUALITY_LOW));
                  mediaRecorder.setOrientationHint(270);
              } else if (cameraId == 0) {
                  mediaRecorder.setProfile(CamcorderProfile
                          .get(CamcorderProfile.QUALITY_HIGH));
                  mediaRecorder.setOrientationHint(orientation);
              }
  
              prRecordedFile = new File(Environment.getExternalStorageDirectory()
                      + "/VideoApp/Videos/"
                      + String.valueOf(System.currentTimeMillis()) + ".mp4");
              mediaRecorder.setOutputFile(prRecordedFile.getPath());
  //            mediaRecorder.setVideoSize(320, 240);
  
              mediaRecorder.setVideoFrameRate(15); // might be auto-determined
  
              mediaRecorder.setMaxDuration(900000); // Set max duration 900
              // sec=15min.
              mediaRecorder.setMaxFileSize(500000000); // Set max file size
              mediaRecorder.setPreviewDisplay(myCameraSurfaceView.getHolder()
                      .getSurface());
  
              mediaRecorder.prepare();
          } catch (IllegalStateException e) {
              releaseMediaRecorder();
              return false;
          } catch (Exception e) {
              releaseMediaRecorder();
              return false;
          }
          return true;
      }
      enter code here
  </code></pre>