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 ?

- 121,568
- 97
- 310
- 388

- 3,580
- 4
- 27
- 33
-
what about set `android:screenOrientation="landscape"` of your CameraActivity in the manifest – IgniteCoders Nov 17 '16 at 19:52
5 Answers
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.
-
9`mediaRecorder.setOrientationHint` only changes the orientation of the output video, not the orientation of the preview. – Cat May 10 '14 at 20:06
-
4In 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
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));

- 11,995
- 10
- 76
- 85

- 640
- 7
- 19
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!!

- 654
- 6
- 13
-
2this 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
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));
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!

- 53
- 3
-
2doesn'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 Oct 15 '14 at 11:29