gpt4 book ai didi

android - 检测视频是否以纵向/横向拍摄

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:00:52 26 4
gpt4 key购买 nike

我想确认我所做的确实是正确的方法,因为某些元素的行为是意外的。

首先,我有横向和纵向布局,据我了解,这样做会自动检测手机是否处于纵向/横向模式:

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

然后,当用户从图库中选择视频时,我检查视频是横向还是纵向,方法是(在 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);
}

我已经对此进行了测试并且它工作正常,但我注意到我的一些 xml 元素(播放按钮)放置不正确。

所以我的应用流程是:

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

我的问题

这是执行此操作的正确方法吗?如果是,是否有任何原因导致某些 xml 元素放置不正确?


编辑 1:

我注意到这只会在 Activity 首次启动时发生,如果我按下后退按钮并再次选择相同的视频,布局将完全符合我的要求。


编辑 2:

我还注意到,只有当先前的 Activity (MainActivity) 与所选视频的方向相同时才会发生这种情况。

最佳答案

这是我最后做的。

我没有使用 MediaMetadataRetriever 获取宽度和高度,而是首先从视频文件中检索一个 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");

}
}

关于android - 检测视频是否以纵向/横向拍摄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45294791/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com