how to change video orientation in MediaRecorder to portrait
Asked Answered
S

5

31

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 ?

Shari answered 10/10, 2010 at 16:12 Comment(1)
what about set android:screenOrientation="landscape" of your CameraActivity in the manifestPractical
L
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.

Lubricious answered 22/8, 2011 at 2:6 Comment(5)
mediaRecorder.setOrientationHint only changes the orientation of the output video, not the orientation of the preview.Prosopopoeia
In addition, setOrientationHint only works for MPEG4 streams. Others (like MPEG2TS) do not implement this option (it gets silently ignored).Caroylncarp
@Prosopopoeia you are right, any idea about how to fix that on the preview ?!!Shack
@ Lekensteyn how to apply setOrientationHint() for MPEG2TS format while recording video.Solent
Note the setOrientationHint only works for 2 formats: "OutputFormat.MPEG_4" and "OutputFormat.THREE_GPP". more info at : developer.android.com/reference/android/media/…Palmitin
S
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));
Stock answered 24/12, 2016 at 9:7 Comment(0)
D
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!!

Dander answered 20/3, 2012 at 18:30 Comment(1)
this code will fail UNLESS you call mCamera.unlock(); before recorder.setCamera(mCamera): developer.android.com/reference/android/hardware/…Prosopopoeia
P
4
 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));
Phira answered 11/8, 2017 at 5:33 Comment(0)
R
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!

Rasorial answered 8/9, 2012 at 1:47 Comment(1)
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.Centi

© 2022 - 2024 — McMap. All rights reserved.