how to change video orientation in MediaRecorder to portrait

╄→尐↘猪︶ㄣ 提交于 2019-11-28 18:40:55

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.

Sagar Aghara

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

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

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!

 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));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!