Android MediaMetadataRetriever错误的视频高度和宽度 [英] Android MediaMetadataRetriever wrong video height and width

查看:153
本文介绍了Android MediaMetadataRetriever错误的视频高度和宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取视频的高度和宽度,为此我正在使用MediaMetadataRetriever类.在大多数情况下,它是正确的,但是在少数情况下,高度和宽度可以互换.

I want to retrieve height and width of video, I am using MediaMetadataRetriever class for this. It is working correct for most of the case, but for few case height and width are interchanged.

我认为这可能是由于方向改变而发生的.

I think this might be happening because of orientation change.

我当前的代码:

MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
        metaRetriever.setDataSource(videoPath);
        videoHeight = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
        videoWidth = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);

如何获取正确的值?谢谢

How can i get correct values? Thank you

推荐答案

最后一天左右,我一直试图自己解决这个问题,最终我不得不通过实验来解决它.

I've been trying to figure this out myself for the last day or so, I eventually had to solve it experimentally.

            File file = new File(path);
            if (file.exists()) {
                MediaMetadataRetriever retriever = new MediaMetadataRetriever();
                retriever.setDataSource(file.getAbsolutePath());
                String metaRotation = retriever.extractMetadata(METADATA_KEY_VIDEO_ROTATION);
                int rotation = metaRotation == null ? 0 : Integer.parseInt(metaRotation);
                Log.i("Test", "Rotation = " + rotation);
            }

如果旋转角度为90或270,宽度和高度将被转置.

If the rotation is 90 or 270 the width and height will be transposed.

这篇关于Android MediaMetadataRetriever错误的视频高度和宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆