29

When I record video by MediaRecorder, it always records in landscape mode, regardless of real device orientation. How to force MediaRecorder/Camera use real orientation ?

Jonas
  • 97,987
  • 90
  • 271
  • 355
Vladimir Berezkin
  • 3,367
  • 4
  • 24
  • 32

5 Answers5

20

refer to Camera.Parameters.setRotation() for more information.

There is an example there and instead of calling setRotation(rotation) try to call mediaRecorder.setOrientationHint(rotation) when recording video.

artsylar
  • 2,500
  • 4
  • 31
  • 48
  • 8
    `mediaRecorder.setOrientationHint` only changes the orientation of the output video, not the orientation of the preview. – Cat May 10 '14 at 20:06
  • 4
    In addition, `setOrientationHint` only works for MPEG4 streams. Others (like MPEG2TS) do not implement this option (it gets silently ignored). – Lekensteyn Jun 01 '14 at 21:35
  • @Cat you are right, any idea about how to fix that on the preview ?!! – Muhammed Refaat Nov 05 '17 at 11:18
  • @ Lekensteyn how to apply setOrientationHint() for MPEG2TS format while recording video. – Ankit Dubey Jul 04 '19 at 14:18
  • Note the setOrientationHint only works for 2 formats: "OutputFormat.MPEG_4" and "OutputFormat.THREE_GPP". more info at : https://developer.android.com/reference/android/media/MediaRecorder#setOrientationHint(int) – Aviram Fireberger Oct 27 '20 at 12:16
14

Add the following two lines of code:

Camera.setDisplayOrientation(90); // use for set the orientation of the preview
mRecorder.setOrientationHint(90); // use for set the orientation of output video

before:

mRecorder.setCamera(mCamera);

Full example:

mRecorder = new MediaRecorder();

// Both are required for Portrait Video
mCamera.setDisplayOrientation(90);
mRecorder.setOrientationHint(90);

// Step 1: Unlock and set camera to MediaRecorder
mCamera.unlock();
mRecorder.setCamera(mCamera);

// Step 2: Set sources
mRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
mRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);

// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
mRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_480P));
Kurt Van den Branden
  • 9,082
  • 8
  • 60
  • 72
Sagar Aghara
  • 600
  • 7
  • 19
8

Take a look at the documentation here

http://developer.android.com/guide/topics/media/camera.html#capture-video

The most common pitfall with the this example is the setCamera() . YOU MUST SET THE CAMERA IMMEDIATELY AFTER MAKING THE MediaRecorder otherwise you will get errors.

    Camera mCamera = getCameraInstance();
    // adjust the camera the way you need
    mCamera.setDisplayOrientation(90);

    MediaRecorder recorder = new MediaRecorder();

    mCamera.unlock();
    recorder.setCamera(mCamera);

    recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
    recorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
    recorder.setOutputFile(filePath);

    // add any limits
    recorder.setMaxDuration(50000); // 50 seconds
    recorder.setMaxFileSize(5000000); // Approximately 5 megabytes 

I hope this helps someone. Good Luck!!

Laith Alnagem
  • 625
  • 6
  • 12
  • 2
    this code will fail UNLESS you call `mCamera.unlock();` before `recorder.setCamera(mCamera)`: http://developer.android.com/reference/android/hardware/Camera.html#unlock() – Cat May 10 '14 at 20:23
3

I've stuck with this problem before, too. I found that you can use the function setOrientationHint (API 9). Call this function before you call MediaRecorder.prepare(). You can setup the orientation degree for your output video.

Hope it helps, good luck!

tomelf
  • 53
  • 3
  • 2
    doesn't actually rotate the video to correct orientation, it's just a flag that is set to video which some mediaplayers like vlc ignore. – Nima G Oct 15 '14 at 11:29
3
 mMediaRecorder = new MediaRecorder();
        mServiceCamera.setDisplayOrientation(90);
        mMediaRecorder.setOrientationHint(90);
        mServiceCamera.unlock();
        mMediaRecorder.setCamera(mServiceCamera);
        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));