2

I'm currently detecting the width and height of a selected video by doing the following:

MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
    mediaMetadataRetriever.setDataSource(this, mVideoUri);
    String height = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
    String width = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);

The activity in manifest:

<activity android:name=".TrimVideoActivity"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen"
        />

If I toast the the width and height it always returns w : 1920 h : 1080 no matter what the dimensions of the video are. I think it is returning the width and height of the device instead.

Is there something that I'm missing or doing wrong?


EDIT

By following the link that @VladMatvienko suggested I was able to get the correct width and height of the video file, this is how I implemented it:

MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    Bitmap bmp = null;
    retriever.setDataSource(this, mVideoUri);
    bmp = retriever.getFrameAtTime();

    String videoWidth = String.valueOf(bmp.getWidth());
    String videoHeight = String.valueOf(bmp.getHeight());

Now I want to rotate the screen depending on the result (width/height), I tried it by doing the following:

int w = Integer.parseInt(videoWidth);
    int h = Integer.parseInt(videoHeight);


    if (w > h) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);


    } if(w < h) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    }

But, the screen always gets rotated to landscape, instead of being set to portrait when the width is smaller than the height?

HB.
  • 4,116
  • 4
  • 29
  • 53

1 Answers1

0

I decided to add a answer explaining how I fixed this issue.

The issue with my initial approach:

MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
mediaMetadataRetriever.setDataSource(this, mVideoUri);
String height = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
String width = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);

The problem with this is that the video might not have the metadata that I'm looking for, which will result a nullpointerexception.

To avoid this issue I can get one frame from the video (as a bitmap) and get the width and height from that bitmap by doing the following:

MediaMetadataRetriever retriever = new MediaMetadataRetriever();
Bitmap bmp;
retriever.setDataSource(this, mVideoUri);
bmp = retriever.getFrameAtTime();

But, this brings up another issue, that you might not have. In my case I would like to get the first/nearest frame to the start, because if the screen was rotated during the capturing of the video, then the width/height of the frame will change, so I just added 1 to getFrameAtTime(1).


Now I can rotate the screen depending of the width and height of my video file by doing:

try {
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    Bitmap bmp;
    retriever.setDataSource(this, mVideoUri);
    bmp = retriever.getFrameAtTime(1);

    int videoWidth = bmp.getWidth();
    int videoHeight = bmp.getHeight();

    if (videoWidth > videoHeight) {
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }
    if (videoWidth < videoHeight) {
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

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

Of course the above will be called from within onCreate.

Hope this helps someone out there.

HB.
  • 4,116
  • 4
  • 29
  • 53