umesh-kushwaha / Android_Video_Recording_portrait

Video recording app, complete orientation handling, video recorded in portrait mode and also playing in portrait mode. Tested in samsung, nexus and sony . Run time permission handling implemented.
Apache License 2.0
3 stars 0 forks source link

Video playback rotated by 90 degrees on Samsung S4 mini with KitKat #1

Open johan-schoeman opened 8 years ago

johan-schoeman commented 8 years ago

Hi Umesh

Tried your project but video playback was turned by 90 degrees when I used VLC and VideoPlayer to play back the recorded video that was recorded in Portrait mode. I have done the following mod to CameraPreview.java

public static int surfacerot = 0;                                     //added by Johan
public static int displayrot = 0;                                     //added by Johan

public void setCameraRotation() {
    try {

        Camera.CameraInfo camInfo = new Camera.CameraInfo();

        if (VideoCaptureActivity.cameraId == 0)
            Camera.getCameraInfo(Camera.CameraInfo.CAMERA_FACING_BACK, camInfo);
        else
            Camera.getCameraInfo(Camera.CameraInfo.CAMERA_FACING_FRONT, camInfo);
        int cameraRotationOffset = camInfo.orientation;
        // ...

        Camera.Parameters parameters = mCamera.getParameters();

        int rotation = ((Activity)mContext).getWindowManager().getDefaultDisplay().getRotation();
        int degrees = 0;
        switch (rotation) {
            case Surface.ROTATION_0:
                degrees = 0;
                break; // Natural orientation
            case Surface.ROTATION_90:
                degrees = 90;
                break; // Landscape left
            case Surface.ROTATION_180:
                degrees = 180;
                break;// Upside down
            case Surface.ROTATION_270:
                degrees = 270;
                break;// Landscape right
        }
        surfacerot = degrees;                                                       //added by Johan
        int displayRotation;
        if (camInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            displayRotation = (cameraRotationOffset + degrees) % 360;
            displayRotation = (360 - displayRotation) % 360; // compensate
            // the
            // mirror
        } else { // back-facing
            displayRotation = (cameraRotationOffset - degrees + 360) % 360;
        }
        displayrot = displayRotation;                                                 //added by Johan

        mCamera.setDisplayOrientation(displayRotation);

        if (camInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            rotate = (360 + cameraRotationOffset + degrees) % 360;
        } else {
            rotate = (360 + cameraRotationOffset - degrees) % 360;
        }

        parameters.set("orientation", "portrait");
        parameters.setRotation(rotate);
        mCamera.setParameters(parameters);

    } catch (Exception e) {

    }
}

In VideoCaptureActivity I have done the following mod:

private boolean prepareMediaRecorder() {

    mediaRecorder = new MediaRecorder();

    mCamera.unlock();
    mediaRecorder.setCamera(mCamera);

    mediaRecorder = new MediaRecorder();
    //mediaRecorder.setOnInfoListener(infoListener);
    mediaRecorder.setCamera(mCamera);
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
    //mediaRecorder.setOutputFile(getVideoFolder()+rnd.nextString()+".mp4");
    //mediaRecorder.setPreviewDisplay(holder.getSurface());
    //Tags the video with a 90° angle in order to tell the phone how to display it
    //mediaRecorder.setOrientationHint(90);

    if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {        //added by Johan
        mediaRecorder.setOrientationHint(mPreview.displayrot);//plays the video correctly                   //added by Johan
    }else{                                                                                                  //added by Johan
        mediaRecorder.setOrientationHint(mPreview.displayrot);                                              //added by Johan
    }

    //mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    //mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    //mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    //mediaRecorder.setOrientationHint(CameraPreview.rotate);

    //mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));

    mediaRecorder.setOutputFile(filePath);
    mediaRecorder.setMaxDuration(15000); // Set max duration 15 sec.
    mediaRecorder.setMaxFileSize(10000000); // Set max file size 1M

    try {
        mediaRecorder.prepare();
    } catch (IllegalStateException e) {
        releaseMediaRecorder();
        return false;
    } catch (IOException e) {
        releaseMediaRecorder();
        return false;
    }
    return true;

}

Now the video plays back correctly in portrait mode when I use VLC (but when using VideoPlayer it is still rotated - seems like VideoPlayer ignores the composite matrix containing the rotation angle in the output video).

LSDsl commented 7 years ago

The same problem on Nexus 4 and Android 5.1 - video is rotated (when try play on pc)