简体   繁体   English

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

[英]Detecting if video was taken in portrait/landscape

I would like to confirm that what I am doing is indeed the correct way as some elements behave unexpected. 我想确认我正在做的事情确实是正确的方式,因为一些元素表现出意想不到的。

First, I have a landscape and portrait layout, as I understand, doing this will automatically detect if the phone is in portrait/landscape mode: 首先,我有一个横向和纵向布局,据我所知,这样做会自动检测手机是否处于纵向/横向模式:

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

Then when the user selects a video from the gallery, I check if the video was taking in landscape or portrait, by doing this (inside OnCreate ): 然后,当用户从图库中选择视频时,我会通过执行此操作(在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 have tested this and it works fine, but I noticed some of my xml elements (play button) is placed incorrectly. 我测试了这个并且它工作正常,但我注意到我的一些xml元素(播放按钮)放置不正确。

So my app flow is: 所以我的应用程序流程是:

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

My Question 我的问题

Is this the correct way of doing this and if it is, is there any reason why some of the xml elements get placed incorrectly? 这是否是正确的方法,如果是这样,是否有任何理由为什么某些xml元素放置不正确?


EDIT 1: 编辑1:

I noticed that this only happens when the activity is launched for the first time, if I press the back button and select the same video again, the layout is perfectly like I want it to be. 我注意到这只发生在第一次启动活动时,如果我按下后退按钮再次选择相同的视频,布局就像我想要的那样。


EDIT 2: 编辑2:

I have also noticed that this only happens if the previous activity (MainActivity) was in the same orientation than what the selected video is. 我还注意到,只有当前一个活动(MainActivity)与所选视频的方向相同时,才会发生这种情况。

Here is what I ended up doing. 这就是我最终做的事情。

Instead of using MediaMetadataRetriever to get the width and height, I first retrieve a Bitmap from the video file and then setting the orientation according to the width and hight of the Bitmap , as shown below: 我首先从视频文件中检索一个Bitmap ,然后根据Bitmap的宽度和高度设置方向,而不是使用MediaMetadataRetriever来获取宽度和高度,如下所示:

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

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以人像拍摄的照片被横向保存 - Photos taken in portrait are being saved in landscape 以纵向模式拍摄的旋转视频 - Rotating video taken in portrait mode 以人像视角横向录制视频 - Recording video in landscape with portrait view 即使以肖像拍摄,三星机器人拍摄的图库图像也始终处于风景中 - Gallery images taken on Samsung androids always in landscape even if taken in portrait 虽然我是以人像录制的,但在横向播放视频 - Video play in landscape although i recorded in portrait android在人像活动中录制风景视频 - android recording a landscape video in a portrait activity 如何检查照片是在Android的横向还是纵向模式下拍摄的? - How to check whether the photo was taken in landscape or portrait mode in Android? 在人像模式下拍摄的图像应在android中保留为横向模式 - image taken when in portrait mode should retain in landscape mode in android 以人像模式从相机拍摄的加载到画布上的照片为横向 - Photos loaded onto canvas taken from a camera in portrait mode are landscape 检测屏幕何时处于纵向或横向模式以及跨设备的屏幕边缘 - Detecting when screen is in portrait or landscape mode and the edges of a screen across device
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM