Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting if video was taken in portrait/landscape

I would like to confirm that what I am doing is indeed the correct way as some elements behave unexpected.

First, I have a landscape and portrait layout, as I understand, doing this will automatically detect if the phone is in portrait/landscape mode:

- layout
   - activity_video_player.xml
 - layout-land
   - activity_video_player.xml

Then when the user selects a video from the gallery, I check if the video was taking in landscape or portrait, by doing this (inside OnCreate):

int w;
int h;

MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
mediaMetadataRetriever.setDataSource(this, videoURI);
String height = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
String width = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
w = Integer.parseInt(width);
h = Integer.parseInt(height);

if (w > h) {  
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

I have tested this and it works fine, but I noticed some of my xml elements (play button) is placed incorrectly.

So my app flow is:

MainActivity --> SelectvidButton --> Gallery Intent --> VideoPlayActivity

My Question

Is this the correct way of doing this and if it is, is there any reason why some of the xml elements get placed incorrectly?


EDIT 1:

I noticed that this only happens when the activity is launched for the first time, if I press the back button and select the same video again, the layout is perfectly like I want it to be.


EDIT 2:

I have also noticed that this only happens if the previous activity (MainActivity) was in the same orientation than what the selected video is.

like image 816
ClassA Avatar asked Jul 25 '17 05:07

ClassA


People also ask

How can we detect the orientation of the screen?

You can detect this change in orientation on Android as well as iOS with the following code: var supportsOrientationChange = "onorientationchange" in window, orientationEvent = supportsOrientationChange ? "orientationchange" : "resize"; window.

Can you flip a video from portrait to landscape?

Click on the "Video" tab (at the bottom of the window), and choose an output format; or click "Target Format" (in the bottom-left corner of the window) to select an output format. Step 4. Click "Rotate" in the editing bar. Then click "Rotate Left" or "Rotate Right" by 90 degrees to convert portrait video to landscape.

Is portrait or landscape better for video?

When you're deciding how to film your video, it's important to consider your target audience and where they'll be watching. If your content is going on your Instagram story, keep it vertical. If it's going on YouTube, make it horizontal.

What is the difference between portrait and landscape video?

Portrait format refers to a vertical orientation or a canvas taller than it is wide. Landscape usually involves subjects that are too wide to shoot with a portrait orientation, and so, you have to turn the camera sideways, and shoot horizontally.


1 Answers

Here is what I ended up doing.

Instead of using MediaMetadataRetriever to get the width and height, I first retrieve a Bitmap from the video file and then setting the orientation according to the width and hight of the Bitmap, as shown below:

private void rotateScreen() {
    try {
        //Create a new instance of MediaMetadataRetriever
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        //Declare the Bitmap
        Bitmap bmp;
        //Set the video Uri as data source for MediaMetadataRetriever
        retriever.setDataSource(this, mVideoUri);
        //Get one "frame"/bitmap - * NOTE - no time was set, so the first available frame will be used
        bmp = retriever.getFrameAtTime();

        //Get the bitmap width and height
        videoWidth = bmp.getWidth();
        videoHeight = bmp.getHeight();

        //If the width is bigger then the height then it means that the video was taken in landscape mode and we should set the orientation to landscape
        if (videoWidth > videoHeight) {
            //Set orientation to landscape
            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
        //If the width is smaller then the height then it means that the video was taken in portrait mode and we should set the orientation to portrait
        if (videoWidth < videoHeight) {
            //Set orientation to portrait
            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }

    } catch (RuntimeException ex) {
        //error occurred
        Log.e("MediaMetadataRetriever", "- Failed to rotate the video");

    }
}
like image 189
ClassA Avatar answered Nov 16 '22 02:11

ClassA