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 ?
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));