简体   繁体   English

如何根据视频的高度和宽度设置屏幕方向

[英]How to set screen orientation according to a video's height and width

I am using videoview , in the onPrepared method and it returns the media player height and width.我在onPrepared方法中使用videoview ,它返回媒体播放器的高度和宽度。 Then I check if the width of the video is greater than the height and rotate the screen.然后我检查视频的宽度是否大于高度并旋转屏幕。 The problem is that if I rotate the screen orientation, it again calls the onCreate method and then recalls all of the code so it takes time to initialize the video again.问题是,如果我旋转屏幕方向,它会再次调用onCreate方法,然后调用所有代码,因此再次初始化视频需要时间。

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the layout from video_main.xml
        setContentView(R.layout.play_video_activity);

        // Insert your Video URL
        String VideoURL = "http://www.androidbegin.com/tutorial/AndroidCommercial.3gp";

        // Find your VideoView in your video_main.xml layout
        final VideoView videoview = (VideoView) findViewById(R.id.VideoView);
        // Execute StreamVideo AsyncTask

        // Create a progressbar
        final ProgressDialog pDialog;
        pDialog = new ProgressDialog(PlayVideoActivity.this);
        // Set progressbar title
        pDialog.setTitle("Android Video Streaming Tutorial");
        // Set progressbar message
        pDialog.setMessage("Buffering...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        // Show progressbar
        pDialog.show();

        try {
            // Start the MediaController
            MediaController mediacontroller = new MediaController(
                    PlayVideoActivity.this);
            mediacontroller.setAnchorView(videoview);
            // Get the URL from String VideoURL
            Uri video = Uri.parse(VideoURL);
            videoview.setMediaController(mediacontroller);
            videoview.setVideoURI(video);

        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }

        videoview.requestFocus();
        videoview.setOnPreparedListener(new OnPreparedListener() {
            // Close the progress bar and play the video
            public void onPrepared(MediaPlayer mp) {
                pDialog.dismiss();
                videoview.start();

                if(mp.getVideoWidth()> mp.getVideoHeight())
                {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                }
            }
        });
    }

Add android:configChanges="orientation" to your activity in manifest file在清单文件android:configChanges="orientation"到您的活动中

Declare variables in your activity在您的活动中声明变量

boolean orienChanged = false;
int lastOrientation = 0;

Override method覆盖方法

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    int orientation = newConfig.orientation;
    if(orientation != lastOrientation){
       orienChanged  = true;
       lastOrientation = orientation ;
    }

}

Check the variable orienChanged in onCreate() for only initial time it will be false,otherwise it must be true.仅在初始时间检查onCreate()的变量orienChanged它将为 false,否则它必须为 true。

Initialize new configuration oncreate... and make newConfigOD valiable global.在创建时初始化新配置...并使 newConfigOD 全局可用。

 Configuration newConfigOD = getResources().getConfiguration();    

Then override this method..然后覆盖这个方法..

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if(videoview.getVideoWidth()> videoview.getVideoHeight())  {
        if (newConfigOD.orientation != Configuration.ORIENTATION_LANDSCAPE) {
         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } 
    } else {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}

Try this... this should work as per your requirement.试试这个……这应该按照你的要求工作。 And remove this if condition from onCreate()并从 onCreate() 中删除这个 if 条件

Auto set orientation according to video scale.根据视频比例自动设置方向。

  1. Implement addVideoListener()实现 addVideoListener()

     videoPlayer.addVideoListener(new VideoListener() { @Override public void onVideoSizeChanged(int width, int height, int unappliedRotationDegrees, float pixelWidthHeightRatio) { } @Override public void onRenderedFirstFrame() {} });
  2. According to height & width set conditions根据高宽设置条件

     if(width>height){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } if(height>width){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); }
  3. Your final objective would be like你的最终目标是

    videoPlayer.addVideoListener(new VideoListener() { @Override public void onVideoSizeChanged(int width, int height, int unappliedRotationDegrees, float pixelWidthHeightRatio) { if(width>height){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } if(height>width){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } } @Override public void onRenderedFirstFrame() {} });

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

相关问题 如何根据另一个视图的高度/宽度按比例设置视图高度/宽度? - How to set View height/width according to another View's height/width by ratio? Android:如何根据屏幕设置标题的高度或宽度 - Android: How to set header's height or width in terms of screen Android屏幕方向(根据手机屏幕宽度) - Android screen orientation according to mobile screen width 如何根据phonegap中的屏幕方向设置“div”大小 - how can i set the “div” size according to screen orientation in phonegap 如何设置Android视频的宽度和高度 - How to Set android video width and height 如何设置MediaRecorder的视频大小以完全适合设备屏幕的宽度和高度? - How to set video size of MediaRecorder to fill perfectly to width and height of device screen? 如何在Libgdx中设置Screen的宽度和高度? - How to set the width and height of Screen in Libgdx? 根据屏幕高度设置布局 - Set the Layout according to the Height of Screen 如何在Android的横向方向中设置宽度适合屏幕(使用TouchImageView) - How to set Width fit to Screen in Landscape Orientation (Using TouchImageView) in android 如何在Android中根据设备dpi设置与宽度成比例的高度 - How to set height proportional to width according to device dpi in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM