1

I am doing android Video Camera Application. It should have capability of recording with back and front camera. I am done with back camera and everything is fine. But when I record with front camera, video plays upside down. I have to play the videos recorded in my application itself. I am using VideoView, How to set orientation or make it play correctly?. Video when played in default media player also playing upside down. Tried sending the video to iPhone and checked but still it plays upside down. I have setOrientationHint to MediaRecorder but no solution.

All My Code is similar to agargenta's Video Capture Demo in which I have done customization.

Issues seems to be strange and I am struck.

this.mediaRecorder = new MediaRecorder();
        this.mediaRecorder.setCamera(this.mCamera);
        this.mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
        this.mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        if (Build.VERSION.SDK_INT <= 10) {
            CamcorderProfile camcorderProfile = CamcorderProfile
                    .get(CamcorderProfile.QUALITY_HIGH);
            camcorderProfile.videoFrameWidth = 320;
            camcorderProfile.videoFrameHeight = 480;
            // camcorderProfile.videoFrameRate = 15;
            camcorderProfile.videoCodec = MediaRecorder.VideoEncoder.H264;
            // camcorderProfile.audioCodec = MediaRecorder.AudioEncoder.DEFAULT;
            camcorderProfile.fileFormat = MediaRecorder.OutputFormat.MPEG_4;
            this.mediaRecorder.setProfile(camcorderProfile);
mediaRecorder.setOrientationHint(90);
        } else {
            if (tgbSwitchCamera.isChecked()) {
                mediaRecorder
                        .setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
                // mediaRecorder.setVideoSize(320, 480);
                mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
                mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
                CameraInfo cameraInfo = new CameraInfo();
                Camera.getCameraInfo(CameraInfo.CAMERA_FACING_FRONT, cameraInfo);
                rotation = (cameraInfo.orientation - 180 + 360) % 360;
                mediaRecorder.setOrientationHint(rotation);

            } else {
                CamcorderProfile camcorderProfile = CamcorderProfile
                        .get(CamcorderProfile.QUALITY_LOW);
                camcorderProfile.videoFrameWidth = 640;
                camcorderProfile.videoFrameHeight = 480;
                camcorderProfile.videoCodec = MediaRecorder.VideoEncoder.H264;
                // camcorderProfile.audioCodec =
                // MediaRecorder.AudioEncoder.DEFAULT;
                camcorderProfile.fileFormat = MediaRecorder.OutputFormat.MPEG_4;
                this.mediaRecorder.setProfile(camcorderProfile);
mediaRecorder.setOrientationHint(90);
            }
        }
        this.mediaRecorder.setOutputFile(this.initFile().getAbsolutePath());
        this.mediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());

        try {
            this.mediaRecorder.prepare();

            // start the actual recording
            // throws IllegalStateException if not prepared
            Handler h = new Handler();
            h.postDelayed(new Runnable() {

                @Override
                public void run() {
                    this.mediaRecorder.start();
                    pgv.setStart(true);
                    pgv.invalidate();
                    CountDownTimer cdt = new CountDownTimer(5000, 1000) {

                        @Override
                        public void onTick(long millisUntilFinished) {
                        }

                        @Override
                        public void onFinish() {
                            if (isRecording) {
                                handler.removeMessages(STOP);
                                stopRecording();
                            }
                        }
                    };
                    cdt.start();
                }
            }, 1000);

            // enable the stop button by indicating that we are recording
        } catch (Exception e) {
            Toast.makeText(this, "cannot record", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
            this.releaseMediaRecorder();
        }
TNR
  • 5,839
  • 3
  • 33
  • 62
  • "*...in which I have done customization.*" Let's see the customizations? – Geobits Jul 29 '13 at 13:55
  • @Geobits, Customization in the sense, not functionality wise it is only design wise. – TNR Jul 29 '13 at 14:40
  • Well, it can't hurt to see. The demo you linked to is bare-bones basic, and there's not much to see. Of course, it could be a hardware- or platform-specific problem, too. However, you haven't told us what you're trying it on. The more relevant details you give, the more help you'll get. – Geobits Jul 29 '13 at 14:43
  • @Geobits, I have added my entire code that does recording. startRecording and stopRecording are from the above demo – TNR Jul 29 '13 at 14:58

1 Answers1

8

This line will specifically rotate the video 180 degrees:

rotation = (cameraInfo.orientation - 180 + 360) % 360;

Unless you have a reason for doing this that I'm not seeing, that's why your video is upside-down.

Geobits
  • 22,218
  • 6
  • 59
  • 103