为时已晚,但可能会对未来的访客有所帮助。
以下行将返回视频的旋转。四个可能的值是 0,90,180,270。
rotation = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION));
如果旋转为 90 或 270,宽度和高度将被调换。
Android MediaMetadataRetriever wrong video height and width
如果旋转为 90 或 270,则滑动宽度和高度。
if(rotation == 90 || rotation == 270)
{
int swipe = width;
width = height;
height = swipe;
}
以下是完整的代码。我将结果与 MXPlayer 比较结果是相同的。
public static String getVideoResolution(File file)
{
try
{
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(file.getPath());
int width = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
int height = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
int rotation = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
rotation = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION));
}
retriever.release();
if(rotation != 0)
{
if(rotation == 90 || rotation == 270)
{
int swipe = width;
width = height;
height = swipe;
}
}
return width + " x " + height;
}
catch (Exception e)
{
e.printStackTrace();
return "Information not found.";
}
}
以下是设置屏幕方向的代码。
int width;
int height;
int rotation = 0;
try {
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(filePath);
width = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
height = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
rotation = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION));
}
retriever.release();
if(rotation != 0)
{
if(rotation == 90 || rotation == 270)
{
int swipe = width;
width = height;
height = swipe;
}
}
this.setRequestedOrientation(width < height ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT : ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
catch (Exception e){
e.printStackTrace();
}
在低于 Build.VERSION_CODES.JELLY_BEAN_MR1 的版本上运行的设备呢?
回答:我不知道。但上面的代码适用于大多数设备。
以下是最佳答案:https://stackoverflow.com/a/56337044/8199772