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