Detect if the video was in portrait / landscape orientation

I would like to confirm that what I am doing is really true, as some elements behave unexpectedly.

Firstly, I have a landscape and a portrait layout, as I understand it, this will automatically determine if the phone is in portrait / landscape mode:

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

Then, when a user selects a video from the gallery, I check if the video takes a 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 tested this and it works fine, but I noticed that some of my xml elements (play button) are not placed correctly.

So, the flow of my application:

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

My question

This is the right way to do this, and if so, is there a reason why some xml elements do not fit correctly?


EDIT 1:

, , , "" , , .


2:

, , (MainActivity) , .

+5
1

.

, MediaMetadataRetriever , Bitmap , Bitmap, :

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");

    }
}
+2

Source: https://habr.com/ru/post/1682183/


All Articles