Junior Programmer
Junior Programmer

Reputation: 420

Android - Recorded Video misoriented

I have a function in an app that is to record a certain length of video using Front-facing camera.

The recording is fine , however, the orientation is not correct. I have searched lots of time but many questions of this are mentioned only on picture

Part that related to Camera and MediaRecorder are provided below

public void onCreate(Bundle savedInstanceState) {
    // Some of the codes are not shown
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    recorder = new MediaRecorder();
    holder = cameraView.getHolder();

}
private void initRecorder() {
    recorder.setCamera(camera);
    recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
    CamcorderProfile cpLow = CamcorderProfile.get(1,
            CamcorderProfile.QUALITY_LOW);
    recorder.setProfile(cpLow);
    mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
    mFileName += "/youraudiofile.mp4";
    recorder.setOutputFile(mFileName);
    recorder.setMaxDuration(50000); 
    recorder.setMaxFileSize(5000000);
}
private void prepareRecorder() {
    recorder.setPreviewDisplay(holder.getSurface());
    try {
        recorder.prepare();
    } catch (IllegalStateException e) {
        e.printStackTrace();
        finish();
    } catch (IOException e) {
        e.printStackTrace();
        finish();
    }
}
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open(findFrontFacingCamera());
camera.unlock();
initRecorder();
prepareRecorder();

}   

The above codes work, and during the record I can get a screen like this record

Instead, the resulting video will display like this result

How can I correct this?

Upvotes: 0

Views: 79

Answers (2)

DDSports
DDSports

Reputation: 400

You should probably be using this:

recorder.setOrientationHint(CamOrientationDegrees);

where 'CamOrientationDegrees' is the angle of the device at start of recording. I've found this to work on most devices except the Samsung S3. The S3 ignores it and orientates the video according to the device orientation, which makes the resultant playback angle incorrect when playing back on (eg) VLC (I still haven't found out how to deal with this!).

If you're not using this on an S3, it should work fine, providing you correctly calculate 'CamOrientationDegrees' taking into account that fact that it's using the front-facing camera (you might need to experiment with it a bit).

CamOrientationDegrees should only take the values 0, 90, 180 or 270.

Upvotes: 2

CommonsWare
CommonsWare

Reputation: 1006869

Video is always recorded as though the device is in landscape orientation, even when the device is in portrait.

There are ways that you can post-process the video to reorient it (e.g., ffmpeg), though I have not experimented with these, and they are third-party libraries, not part of the Android SDK.

Upvotes: 0

Related Questions