3

I am trying to record a video in portrait orientation.

Setting the Camera DisplayOrientation to 90 degrees makes the preview to video be displayed in portrait.

But When calling setOrientationHint() with any given number(0,90,180,270) the video that was created is created always in portrait orientation.

When I have tested it on Jellybean and on ICS, the video orientation was the one I set with the setOrientationHint() method.

Here is the MediaRecorder initialization code:

private void initRecorder() {
    Camera camera = Camera.open();
    camera.setDisplayOrientation(90);
    camera.unlock();
    recorder.reset();
    recorder.setCamera(camera);
    recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
    file = new File("/sdcard/test.mp4");
    if (!file.exists()) {
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    recorder.setOrientationHint(90);//doesn't seem to work on 2.3

    recorder.setOutputFile(file.getAbsolutePath());
    recorder.setMaxDuration(30000);
    recorder.setMaxFileSize(1000000);
}

And this is where I prepare the MediaRecorder:

public void surfaceCreated(SurfaceHolder holder) {
    this.holder = holder;
        recorder.setPreviewDisplay(holder.getSurface());
    try {
        recorder.prepare();
    } catch (IllegalStateException e) {
        e.printStackTrace();
        finish();
    } catch (IOException e) {
        e.printStackTrace();
        finish();
    }
}

EDIT:

Tested on:

  1. Samsung Galaxy S running android 2.2.

  2. Samsung Galaxy W running android 2.3.

  3. Samsung galaxy S2 running ICS - worked.

  4. Samsung galaxy s3 running Jelly Bean - worked.

meh
  • 22,090
  • 8
  • 48
  • 58
  • Are you testing on physical devices or emulator? What are you devices? – Hartok Feb 17 '13 at 14:56
  • @Hartok I have edited my answer with the devices, and I couldn't test it on an amulator, atleast not capture a video. I have played the video on my computer to check the orientation. – meh Feb 17 '13 at 16:04
  • have you checked the metadata of the videos? What could be happening: 2.3 only writes the angle into the metadata, but is ignored on playback- but 4.0 ++ will actually rotate the video after recording – Yalla T. Feb 22 '13 at 10:19

1 Answers1

2

setOrientationHint() needs to be called before prepare(). Check if this is happening.

Also note that this method only works for 3gpp and mpeg4 formats and some video players choose to ignore this property completely. The video players you are using might be ignoring it.

Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84
  • It throws an exception otherwise, I am already calling it before the 'prepare' and the format is mp4. And the video player I am using is not ignoring is since it has shown the right orientation when I recorded with a devices running ICS&Jelly Bean. – meh Feb 18 '13 at 08:23
  • which video player are you testing it on? Is it the stock player that comes with the device or a third party player like vlc? – Anup Cowkur Feb 18 '13 at 08:30
  • both on vlc, and I am testing it in a simple activity with a videoview in portrait mode. – meh Feb 18 '13 at 08:52