1

I am working on an app where I have to record a video which is supposed to be in Portrait View specifically and play it in Portrait View as well. I'm using mediaRecorder to record the video.

Have used this site for reference: https://examples.javacodegeeks.com/android/core/android-video-capture-example/

and made slight changes to suit my requirements:

Here is the code:

For the camera I use these settings:

mCamera = Camera.open(findFrontFacingCamera());
            mCamera.setDisplayOrientation(90);
            Camera.Parameters params= mCamera.getParameters();
            params.set("rotation", 90);
            params.set("orientation", "portrait");
            mCamera.setParameters(params);
            mPreview.refreshCamera(mCamera);

The problem I'm facing right now is that video gets recorded (what seems to be) in Portrait View but when I play it using any player for eg. MX Player I get a video that is recorded/played in Landscape View.

Here are the screens for reference:

The recording Screen

The video played: enter image description here

I have used the setDisplayOrientation(90) parameter but its not working. How do I achieve that I'm trying to achieve?

Suhas Shelar
  • 963
  • 2
  • 10
  • 23

1 Answers1

1

Unlike iOS camera, we cannot truly choose portrait camera frame orientation on Android.

Camera.setDisplayOrientation() only refers to the orientation of live video preview from camera; the actual frame data is never rotated on Android. You can see it if you use an onPreviewFrame() callback. Camera.setRotation() only effects the onPictureTaken() callback, and on majority of devices only sets an EXIF rotation flag.

Setting "rotation" and "orientation" explicitly by name is dangerous: some devices may recognize these keys, other will silently ignore them, while yet other ROMs may throw a RuntimeException when the Camera.setParameters() params with unexpected key or value is received. But at any rate, I have never seen an Android device that changes the recorded video frames when any of these flags.

You have the MediaRecorder.setOrientationHint() API, but it will not rotate the frames of recorded video, only put a flag in the video header.

So, unfortunately you must rotate the video on the receiving side. This depends on the way you render your video, and yes, you can check the orientation hint if you want.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • Any suggestion as to how I can do it by myself ? Any reference source you know of? – Suhas Shelar Apr 11 '16 at 10:14
  • also I have already used setOrientationHint() but its still not working. – Suhas Shelar Apr 11 '16 at 10:17
  • what do you mean "not working"? Do you check this flag on the receiver? – Alex Cohn Apr 11 '16 at 10:25
  • How do you render the video? If you use OpenGL texture, you can apply a transformation matrix, like in http://stackoverflow.com/questions/29518560/inconsistent-video-rotation-when-using-mediacodec – Alex Cohn Apr 11 '16 at 10:30
  • I have simply recorded the video with setOrientationHint(90) and tried playing the video on default player and MX player but in both the video is still getting played in Landscape mode and not Portrait mode. I'm not rendering it myself at this point of time. – Suhas Shelar Apr 11 '16 at 10:55
  • According to http://android.stackexchange.com/questions/61382/is-there-a-video-player-that-supports-the-orientation-flag, MX Player has this support. Please check that the flag is actually set in your video. Note that you must set it early, it will not change the clip that is being recorded! – Alex Cohn Apr 11 '16 at 11:22
  • `mCamera.unlock(); mediaRecorder = new MediaRecorder(); mediaRecorder.setCamera(mCamera); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_480P)); mediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface() mediaRecorder.setOrientationHint(90); ` This is prepared well before I call `mediaRecorder.start()` I confirmed it using a debugger – Suhas Shelar Apr 11 '16 at 12:14
  • 1
    I think I got what the problem is. I had been testing the app on my Nexus 7 running 6.0.1. When I tested on other devices: Samsung S3, Panasonic Eluga One, Asus Zenphone 2, MI4i, Moto G3, I found that its working properly. So I think there's problem with nexus 7. Thanks for the help ! – Suhas Shelar Apr 11 '16 at 13:35